【问题标题】:file download with php (working on apache under localhost)使用 php 下载文件(在 localhost 下使用 apache)
【发布时间】:2009-08-07 20:21:30
【问题描述】:

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

<?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

任何建议为什么会发生这种情况? 谢谢

【问题讨论】:

  • 发布一个不起作用的例子
  • 即使尝试使用相对 url 从同一个地方获取文件,如此评论中发布的代码,它也无法正常工作,导致下载名为“_zip.zip”的文件,该文件实际上并不存在,感谢您的关注

标签: php http-headers download


【解决方案1】:

使用basename 只获取文件名:

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

【讨论】:

  • 感谢 :) 解决了,非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
相关资源
最近更新 更多