使用 php 下载文件(在 localhost 下的 apache 上工作)
我正在尝试创建一个文件下载页面。 当请求时,此页面应提示用户下载文件。 这是该页面的源代码:
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
basename
获取文件名:Use
basename
to get just the file name: