如何使用PHP脚本下载视频

时间:2011-01-07 09:16:45

标签: php

在我的程序中,我想添加一个下载选项来下载当前的straming视频。我试过这段代码:

$psp = "Tom_20_amp__20Jerry_20race-1.flv";
header("Content-type:application/octet-stream");
header("Content-Disposition:attachment;filename=$psp");

但是我收到了这个错误:

  

“C:\ DOCUME~1 \ ADMINI~1 \ LOCALS~1 \ Temp \ Tom_20_amp__20Jerry_20race-1-5.flv”不是有效的FLV文件。

正确播放视频。请指导我

2 个答案:

答案 0 :(得分:6)

更改您的标题:

header("Content-type:application/octet-stream");

致:

header("Content-type: video/flv");

然后你可以这样做:

header("Content-Disposition:attachment;filename=\"$psp\"");
//allways a good idea to let the browser know how much data to expect
header("Content-length: " . filesize($psp) . "\n\n"); 
echo file_get_contents($psp); //$psp should contain the full path to the video

答案 1 :(得分:0)

如果您希望允许下载文件而不是流式传输,那么这样的事情应该可行。您显然需要更改路径。

// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="download.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');

修改

在你的情况下,它会是这样的。

// We'll be outputting a video
header('Content-type: video/flv');

// It will be called video.flv
header('Content-Disposition: attachment; filename="video.flv"');

// The PDF source is in original.flv
readfile('original.flv');