首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用X-Sendfile服务的PHP文件

使用X-Sendfile服务的PHP文件
EN

Stack Overflow用户
提问于 2016-05-25 08:50:33
回答 1查看 3.3K关注 0票数 3

我正在建立一个有文件服务脚本的网站。这个脚本允许网站提供pdf、mp3和mp4文件。但是只有PDF和MP3文件起作用。通过点击播放视频,我期待视频文件播放,但它不是。视频控件已被禁用,无法播放。

files.php

代码语言:javascript
复制
<?php
error_reporting(E_All);

$fid = $_GET['fid'];
$ftype = $_GET['ftype']; // e.g. audios, videos, ebooks
$fcat = isset($_GET['cat']) ? $_GET['cat'] . '/' : ''; // e.g. lessons, more
$fext = '';
$fmime = '';

switch ($ftype) {
    case 'ebooks':
        $fext = '.pdf';
        $fmime = 'application/pdf';
        break;
    case 'audios':
        $fext = '.mp3';
        $fmime = 'audio/mp3';
        break;
    default:
        $fext = '.mp4';
        $fmime = 'video/mp4';
        break;
}

// example: audios/lessons/audio1.mp3
$file = $ftype . '/' . $fcat . str_replace('s', '', $ftype) . $fid . $fext;

if (file_exists($file))
{   
    // open the file as binary mode
    $fp = fopen($file, 'rb');

    // send the right headers
    header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header('Pragma: no-cache');
    header('Content-type: ' . $fmime);
    header('Content-Length: ' . filesize($file));

    // dump the file then stop the program
    fpassthru($fp);
    exit;
}
else
{
    die('File loading failed.');
}

video.php

代码语言:javascript
复制
<video src="/products/files.php?fid=1&ftype=videos&cat=lessons" autoplay controls></video>

或者,到地址栏

代码语言:javascript
复制
mydomain.com/products/files.php?fid=1&ftype=videos&cat=lessons

还有人能发现我做错了什么吗?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-01 01:09:27

通过使用X-Sendfile apache模块,我终于解决了这个问题。

代码语言:javascript
复制
<?php
if (file_exists($file))
{
    // send the right headers
    header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header('Pragma: no-cache');
    header('Content-type: ' . $fmime);
    header('Content-Length: ' . filesize($file));

    // Make sure you have X-Sendfile module installed on your server
    // To download this module, go to https://www.apachelounge.com/download/
    header('X-Sendfile: ' . $file);
    exit;
}
else
{
    die('File loading failed.');
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37432123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档