嵌入位于 webroot 之外的 flv 和 swf
Posted
技术标签:
【中文标题】嵌入位于 webroot 之外的 flv 和 swf【英文标题】:embedding flv and swf located outside webroot 【发布时间】:2013-01-04 07:40:08 【问题描述】:我有一个在 webroot 之外上传的脚本。然后通过网站将用户链接到图像文档等。
所以对于图片,链接是:
media.php?file=nameoffile.jpg&user=userid&folder=images
然后用于显示图像:
<img src="media.php?file=nameoffile.jpg&user=userid&folder=images" border="0">
这适用于图像并提供下载文档的链接。
我面临的问题是嵌入,我使用 ffmpeg 将所有允许的视频类型转换为 flv(这些视频经过测试并且效果很好),但是当我尝试嵌入 flv 视频时它永远无法工作(它适用于直接链接文件只是不通过 media.php)。如果可能的话,我还想嵌入 .swf。
我正在使用 jwplayer 嵌入(与文件的直接链接一起工作,而不是通过 media.php)
<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->
<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" >
<param name="movie" value="player.swf" />
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="flashvars" value="media.php?file=nameoffile.flv&user=userid&folder=videos" />
<embed
type="application/x-shockwave-flash"
id="player2"
name="player2"
src="player.swf"
allowscriptaccess="always"
allowfullscreen="true"
flashvars="file=media.php?file=nameoffile.flv&user=userid&folder=videos"
/>
</object>
<script type="text/javascript" src="jwplayer.js"></script>
<!-- END OF THE PLAYER EMBEDDING -->
这里是 media.php:
$path_parts = pathinfo($_SERVER['REQUEST_URI']);
$file = basename(urldecode($_GET['file']));
$user = basename(urldecode($_GET['user']));
$folder = basename(urldecode($_GET['folder']));
$ext = pathinfo($file, PATHINFO_EXTENSION);
$fileDir = 'pathoutsidewebroot';
$filePath = $fileDir . $file;
switch(
$ext)
case "flv": $ctype="video/x-flv"; break;
// adobe
case "pdf": $ctype="application/pdf"; break;
// ms office
case "doc": $ctype="application/msword"; break;
case "rtf": $ctype="application/rtf"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
// open office
case "odt": $ctype="application/vnd.oasis.opendocument.text"; break;
case "ods": $ctype="application/vnd.oasis.opendocument.spreadsheet"; break;
default: $ctype = "application/force-download"; break;
if(in_array($ext, $valid_formats_vid))
if (file_exists($filePath))
header('Content-Type: ' . mime_content_type($filePath));
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
else if(in_array($ext, $valid_formats_img))
if (file_exists($filePath))
header('Content-Type: ' . mime_content_type($filePath));
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
else if(in_array($ext, $valid_formats_docs))
if (file_exists($filePath))
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filePath)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filePath));
set_time_limit(0);
@readfile($filePath) or die("File not found.");
通过 media.php 嵌入的标头
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Disposition:filename=encoded_2012-10-19_22.37.09_1359032866.flv
Content-Length:0
Content-Type:video/x-flv
Date:Thu, 24 Jan 2013 16:26:32 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=88
Pragma:no-cache
Server:Apache/2.2.20 (Ubuntu)
X-Powered-By:PHP/5.3.6-13ubuntu3.8
直接链接到文件的标题(有效的)
Accept-Ranges:bytes
Connection:Keep-Alive
Content-Length:2428614
Content-Type:video/x-flv
Date:Thu, 24 Jan 2013 16:23:54 GMT
ETag:"26ca3d8-250ec6-4d4087c796500"
Keep-Alive:timeout=5, max=100
Last-Modified:Thu, 24 Jan 2013 13:07:00 GMT
Server:Apache/2.2.20 (Ubuntu)
设法通过 media.php 将其更改为这个(但仍然无法正常工作)
header("Content-Type: $ctype");
header('Content-Length: ' . filesize($filePath));
header('Accept-Ranges: bytes');
$now = time( );
$then = gmstrftime("%a, %d %b %Y %H:%M:%S GMT", $now + 365*86440);
header("Expires: $then");
ob_clean();
flush();
readfile($filePath);
Accept-Ranges:bytes
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:2428614
Content-Type:video/x-flv
Date:Thu, 24 Jan 2013 16:44:18 GMT
Expires:Fri, 24 Jan 2014 20:47:38 GMT
Keep-Alive:timeout=5, max=79
Pragma:no-cache
Server:Apache/2.2.20 (Ubuntu)
X-Powered-By:PHP/5.3.6-13ubuntu3.8
【问题讨论】:
【参考方案1】:我设法让它与以下内容一起工作,我还为播放器添加了一个拇指图像(这是通过 ffmpeg 上传的):
header("Content-Type: $ctype");
header('Content-Length: ' . filesize($filePath));
header('Accept-Ranges: bytes');
$now = time( );
$then = gmstrftime("%a, %d %b %Y %H:%M:%S GMT", $now + 365*86440);
header("Expires: $then");
ob_clean();
flush();
readfile($filePath);
$flv_path = 'media.php?file='.$row['cur_image'].'&folder=videos&user='.$row["posted_by"];
$thumb = pathinfo($row['cur_image']);
$thumb_path = 'media.php?file='.$thumb['filename'].'.jpg&folder=videos&user='.$row["posted_by"];
?>
<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->
<div id="mediaplayer_<?php echo $row['p_id']; ?>">JW Player goes here</div>
<script type="text/javascript">
jwplayer("mediaplayer_<?php echo $row['p_id']; ?>").setup(
flashplayer: "jwplayer/jwplayer.flash.swf",
file: "<?php echo $flv_path; ?>",
image: "<?php echo $thumb_path; ?>",
controlbar: "bottom",
width: "380",
height: "200",
primary: "flash",
type: "mp4",
controls: true,
allowscriptaccess: 'always'
);
</script>
<!-- END OF THE PLAYER EMBEDDING -->
【讨论】:
【参考方案2】:其中一种文件读取方法应该可以工作,请查看此链接:http://www.ibm.com/developerworks/library/os-php-readfiles/
修改你的代码,这一行:readfile($filePath);使用其他文件读取功能,我认为流式传输也应该与(PHP fread)一起使用
PHP 手册:fread :提供有用的信息,也请尝试使用第一个用户注释。 http://php.net/manual/en/function.fread.php
【讨论】:
【参考方案3】:问题出在这里:
flashvars="file=media.php?file=nameoffile.flv&user=userid&folder=videos"
flashvars
接收到一个查询字符串,所以这被解释为
file : media.php?file=nameoffile.flv
user : userid
folder : videos
需要对文件参数进行urlencode:
flashvars="file=media.php?file=nameoffile.flv&user=userid&folder=videos"
【讨论】:
这行得通吗? ------flashvars="file=" 是的,我做到了,但它不起作用......只是想知道我是否做对了。根据longtailvideo.com/support/jw-player/jw-player-for-flash-v5/…,输出的 URL 看起来不错 链接证明了我的观点。所以肯定还有另一个问题......你是否改变了flashvars
的两个出现?
是的,我都改了。我的嵌入代码的其余部分可以吗?会不会和 mime_content_type 有关
它可能与标题有关,但我不能指望它,我会尝试检查发送的标题(即使用 FireBug)并将直接访问文件中的标题与来自你的脚本。以上是关于嵌入位于 webroot 之外的 flv 和 swf的主要内容,如果未能解决你的问题,请参考以下文章