PHP下载脚本

发布于 2024-12-08 05:20:25 字数 648 浏览 1 评论 0原文

我正在尝试编写一个 php 脚本,该脚本将在启动下载到客户端之前检查传入的参数。我首先尝试启动下载:

    <?php
    $file =  '/tticon.jpg';

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
?>

当我导航到此脚本时,它显示一个空白页面并且没有任何反应。如何开始下载?

I'm trying to write a php script that will check parameters passed in before it initiates a download to the client. I've started by attempting to just initiate a download:

    <?php
    $file =  '/tticon.jpg';

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
?>

When I navigate to this script it shows a blank page and nothing happens. How do I initiate a download?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千仐 2024-12-15 05:20:25

尝试使用 application/force-download 作为 Content-Type。如果你想在浏览器中显示图像,你可以使用image/jpg

Try using application/force-download as Content-Type. If you want to show the image in the brwoser you can use image/jpg.

野鹿林 2024-12-15 05:20:25
header("Content-type: application/force-download"); 
header("Content-type: application/force-download"); 
红焚 2024-12-15 05:20:25

由于文件的扩展名是“.jpg”,我假设它的 mime 是 image/jpeg

将此行: 替换

header('Content-Type: application/octet-stream');

为:

header('Content-Type: image/jpeg');

如果您确实想强制浏览器下载文件(无论如何,我发现这不太可能):

将此行替换为:

header("Content-Type: application/force-download");

Since the file's extension is ".jpg" I assume it's mime is image/jpeg.

Replace this line:

header('Content-Type: application/octet-stream');

with this:

header('Content-Type: image/jpeg');

If you really want to force the browser to download the file (which I find very unlikely, anyway):

Replace the line with this:

header("Content-Type: application/force-download");
晒暮凉 2024-12-15 05:20:25

您的 $file 看起来很可疑。你的服务器根目录下真的有 tticon.jpg 吗?请记住,PHP 的文件函数在服务器的文件系统上运行,而不是在 Apache 提供的 WEB 目录上运行。 PHP 不会神奇地将站点的文档根目录添加到该路径。它实际上是在服务器文件系统的根目录中查找,而不是在站点的文档根目录中查找。

Your $file looks suspect. Do you really have a tticon.jpg in the root directory of your server? Remember that PHP's file-functions operate on the FILESYSTEM of the server, not the WEB directories that Apache presents. PHP will not magically pre-pend your site's document root to that path. It's literally going to be looking in the root directory of the server's file system, NOT in the document root of your site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文