【问题标题】:PHP - How to deliver a local file efficientlyPHP - 如何有效地传递本地文件
【发布时间】:2018-05-23 11:47:45
【问题描述】:

我开发了一个通过安全 URL 传递文件的脚本,目的是模仿 Amazon S3 的私有文件托管。用户将向服务器发送请求,该服务器生成可用于访问文件的 URL。生成的 URL 在生成后 2 小时后过期。

这是脚本的解密部分:

$data = decrypt($_GET['data']);
    $data_json = json_decode($data,true);

    // print_r($data_json); die;
    $elapsed = strtotime('now') - $data_json['expire'];
    if ($elapsed > URLEXPIRE) {
        echo json_encode(array(
            'status'=>false,
            'error'=>'URL expired.',
            'debug'=>$data,
            'debug2'=>$_GET['data']
        ));
        die;
    }

    $object = OBJECTSTORE . $data_json['path'];
    header('X-Elapsed: ' . $elapsed);
    // header('Content-Type: '.get_mime_type($object));
    header('Content-Type: application/octet-stream');
    // header('Content-Disposition: attachment; filename=' . basename($object));
    if (strpos($data_json['path'],'?download'))
        header('Content-Disposition: attachment;');

    // echo file_get_contents($object);
    readfile($object);
    die;

在我的登台服务器上进行测试期间,脚本运行良好。但是,在交付大型视频文件时在生产中发布时,它似乎很慢。将 URL 作为 HTML 视频标签的源加载需要一段时间。我不知道这是否有帮助,但 decrypt 函数使用 openssl_decrypt 使用 AES-256-CBC 密码。

有什么更有效的方法吗?

【问题讨论】:

  • 您可以将 apache 与 mod_auth_basic 和 https 一起使用。或您喜欢的其他网络服务器。

标签: php openssl php-openssl


【解决方案1】:

在 readfile() 之前验证输出缓冲区是否关闭:添加 ob_end_clean()。 此外, readfile 对于大文件可能效率不高。见Readfile() and large files

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 2018-11-02
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 2012-07-06
    相关资源
    最近更新 更多