PHP脚本来强制下载不工作 [英] PHP Script to Force Download Not Working

查看:152
本文介绍了PHP脚本来强制下载不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的小的PHP code,让我强迫用户下载从我的网站的视频,而不是在浏览器中播放它

I had a simple little php code that allowed me to force users to download videos from my site rather than playing it in the browser

<?php

$path = $_GET['path'];
header('Content-Disposition: attachment; filename=' . basename($path));
readfile($path);

?>

自从我搬到我的网站到新的服务器,似乎有哪里我得到一个 403禁止权限问题您没有权限访问此服务器上/download-stream.php。

php文件设置为相同的权限之前(644)。我不知道为什么它现在这样做。

the php file is set to the same permissions as before (644). I'm not sure why it's doing this now.

推荐答案

我一般使用这样的强制下载。记住,如果你更改下载文件的类型来改变MIME类型。

I generally use something like this to force downloads. Remember to change MIME type if you change the type of file being downloaded.

        $attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/file.zip";
        if (file_exists($attachment_location)) {

            header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
            header("Cache-Control: public"); // needed for i.e.
            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length:".filesize($attachment_location));
            header("Content-Disposition: attachment; filename=file.zip");
            readfile($attachment_location);
            die();        
        } else {
            die("Error: File not found.");
        } 

这篇关于PHP脚本来强制下载不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆