donglv9813 2013-07-02 14:58
浏览 84

php html输出视频,没有扩展到浏览器

I have media files like mp4, ogg etc to server on page. But the problem is due to some security reasons files don't have any extensions although they are media files. So Apache cant recognize the mime type and hence html5 video tags fail to play videos.

What are my options ?

since i don't have any extension, .htaccess doesnot work. If I put file-extension in names then it starts working. Also I have mime type of these files stored in databse.

File strucure:

asdsad/media
werewr/media
23frwe/media

//where media is the file ( could be mp4/ogg/webm) with no extension.
// before / is folder name
// If i change media to media.mp4 , it works

.htaccess file

AddType video/ogg .ogv .ogm .ogg
AddType audio/ogg .oga .ogg
AddType video/mp4 .mp4
AddType video/webm .webm
AddType video/x-m4v .m4v
AddType text/x-vcard .vcf

what if I output file from php instead of linking it from localpath ? will it work ? something like this.

header("Content-type: video/mp4");
echo file_get_contents($pathTmp4);
  • 写回答

1条回答 默认 最新

  • dongping5230 2013-07-02 15:01
    关注
    <?php
    // tell the browser the file type
    header("Content-type: video/mp4");
    // tell the browser the file size
    header(sprint("Content-size: %d", filesize($pathTmp4)));
    // disable output buffering before delivering the file
    while(ob_get_level()) ob_end_clean();
    // send it over
    readfile($pathTmp4);
    // done, track speed and such optional stuff here
    ?>
    

    Consider readfile. Will output directly bypassing the load in memory that file_get_contents does.

    Or just open the file and use fread to send chunks. NEVER load the entire thing in memory before sending it.

    UPDATE

    In terms of speed, you need to test. This will bypass .htaccess issues as it uses PHP to spit out the file. This allows you to control headers and feed the appropriate types to the browser. Downside is the PHP overhead for just delivering a file.

    I'd personally go for fopen + fread + fclose combo as we're talking video files which are large. This also allows you to track sending progress in a DB, track speed of delivery and such. Download tracking in real-time might be of use to you.

    But for anything else (smaller potatoes), I use readfile(). It will not give you updates until it's done. But it does its job.

    You still need to test. Use a video file of 100+ MB and send it. See how memory is doing on the PHP instance and how it behaves overall.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算