doulang9521 2019-04-29 15:32
浏览 116
已采纳

使用php发送文件而不暴露目录

These are my settings:

Web Server: Apache/2.4.25
Web structure:
 /
 |__Video/
         |__ test.mp4
 |__play.php

Currently I am exposing all my videos on my server putting directly the video directory in the apache directory.

What I want to obtain is to move the video outside the Apache directory (assume no permission problems) and trasmit only the requested video through a GET request to play.php like: GET play.php?req=test.mp4

Currently I've written the following code:

$name = $_GET['req']
header('Accept-Ranges: bytes');
header('Content-Length: ' . filesize($name));
header('Content-Type: ' . mime_content_type($name));
header('Content-Disposition: filename="'.$name.'"');
readfile($name);
exit;

What is missing? Why do I get "No supported video media" when I try to access to the file using play.php? Using "mysite.com/Video/test.mp4" I can access directly to the media without problems.

I realized that if I wait a sufficient long time, the browser fully download the file and reproduce it. What I can't obtain is the streaming of the file.

  • 写回答

1条回答 默认 最新

  • drouie2014 2019-04-30 09:05
    关注

    I've found the problem and a possible solution.

    1. The problem is in the fileread, it trasmit the whole file before the browser can process it.
    2. A possible workaround is the use of the X-SendFile module.

    In case you use Debian like me is sufficient to:

    • Install it: sudo apt-get install libapache2-mod-xsendfile
    • Modify your site conf (Default path: /ect/apache2/sites-available) adding the following lines at the beginning (or to the chosen VirtualHost):
    XSendFile on
    XSendFilePath /full/path/to/directory1
    XSendFilePath /full/path/to/directory2
    
    • Restart the web server: sudo systemctl reload apache2 && sudo systemctl restart apache2
    • Update the php script in this way:
    $name = "/full/path/to/directory1/".$_GET['req']
    header('Accept-Ranges: bytes');
    header('Content-Length: ' . filesize($name));
    header('Content-Type: ' . mime_content_type($name));
    header('Content-Disposition: filename="'.$name.'"');
    header('X-Sendfile: $name');
    

    For more information I suggest you to visit this page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 Matlab联合CRUISE仿真编译dll文件报错
  • ¥15 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId