使用 php 下载文件(在 localhost 下的 apache 上工作)

发布于 2024-07-30 06:35:24 字数 468 浏览 2 评论 0原文

我正在尝试创建一个文件下载页面。 当请求时,此页面应提示用户下载文件。 这是该页面的源代码:

<?php
header('Content-disposition: attachment; filename=zip.zip')

header('Content-type: application/zip');

readfile('zip.zip');

?>

这可以正常工作。

当我想从该脚本所在的文件夹中移动文件 zip.zip 时,问题就开始了。我尝试使用相对和绝对 URL,但总是得到奇怪的结果, 浏览器仍然提示下载文件,但不知何故,它只是从我提供的 URI 转换而来的奇怪文件名,例如“.._.._files_zip.zip”而不是 ../../ files/zip.zip

有什么建议吗? 谢谢

I'm trying to create a file download page. This page when requested should prompt the user to download a file. Here is the source code for the page:

<?php
header('Content-disposition: attachment; filename=zip.zip')

header('Content-type: application/zip');

readfile('zip.zip');

?>

This works ok.

The problems starts when I want to move the file zip.zip from the folder where this script is in. I tried using relative and absolute URLs but I always get strange results,
the browser still prompts for file download but somehow it's just an odd file name converted from the URI I supplied somthing like ".._.._files_zip.zip instead of ../../files/zip.zip.

Any suggestions why this happens?
Thanks

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

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

发布评论

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

评论(1

混浊又暗下来 2024-08-06 06:35:25

使用 basename 获取文件名:

$file = '../../files/zip.zip';
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Type: application/zip');
readfile($file);

Use basename to get just the file name:

$file = '../../files/zip.zip';
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Type: application/zip');
readfile($file);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文