PHP文件下载

发布于 2024-09-18 07:46:13 字数 671 浏览 18 评论 0原文

我目前正在构建一个脚本,允许用户通过 URL 下载文件,而无需实际查看文件名或文件存储位置。到目前为止,我已经完成了所有内容,但我需要知道如何调用文件来打开和下载。我目前有一个工作版本(代码如下),但由于某种原因 PHP 损坏了下载。每次我尝试打开下载到桌面的文件时,都会收到损坏的错误消息。当我在服务器本身上打开相同的文件时,该文件工作正常。

网址结构
http://www.example.com/download/file/cjVQv0ng0zr2

启动下载的代码

    $fullpath = BASE_PATH . '../uploads/brochures/' . $vendors['0']['filename'];

    header("Content-type: application/pdf");
    header('Content-disposition: attachment; filename="' . $fullpath . '"');

我是否做错了什么会导致文件损坏?我缺少一两个标题吗?

提前致谢, 杰克

I'm currently building a script that will allow a user to download a file via a URL without actually seeing the filename or where the file is stored. So far I have everything built out, but I need to know how I would go about calling the file to open and download. I currently have a working version (code below), but for some reason the PHP is corrupting the download. Everytime I try to open a file that downloads to my desktop I get a corrupt error message. When I open the same file on the server itself, the file works just fine.

URL Structure:
http://www.example.com/download/file/cjVQv0ng0zr2

Code that initiates the download

    $fullpath = BASE_PATH . '../uploads/brochures/' . $vendors['0']['filename'];

    header("Content-type: application/pdf");
    header('Content-disposition: attachment; filename="' . $fullpath . '"');

Am I doing something wrong that would cause the file to become corrupt? Am I missing a header or two?

Thanks in advance,
Jake

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

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

发布评论

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

评论(4

他是夢罘是命 2024-09-25 07:46:13

您需要在发送标头后调用以下行。

readfile($fullpath); 

并且还像这样调整标题:

header('Content-disposition: attachment; filename="' . basename($fullpath) . '"');

我不确定的一件事是 $fullpath .. 尝试查看您拥有的 $fullpath 是否正确并且您实际上可以到达该文件,这需要是完整的物理路径文件。

我认为添加以下标头也是一个好主意:

header("Content-Transfer-Encoding: binary"); 

You need to call the following line after sending the header.

readfile($fullpath); 

and also adjust in the header like this:

header('Content-disposition: attachment; filename="' . basename($fullpath) . '"');

One thing i am not sure about is the $fullpath .. try to see if the $fullpath you have is correct and you can actually reach the file, this needs to be the full physical path of the file.

I think it would also be a good idea to add the following header as well:

header("Content-Transfer-Encoding: binary"); 
烂柯人 2024-09-25 07:46:13

不久前我也遇到过类似的问题。确保脚本文件中的 " 标记之前或 "?>" 标记之后没有任何额外的空格。就我而言,脚本的最后一个字符是 "\n" 而不是预期的 ">"

I had a similar issue a while back. Make sure you don't have any extra whitespace in your script file, either before the "<?php" tag or after the "?>" tag. In my case the last character of my script was "\n" instead of the expected ">".

离旧人 2024-09-25 07:46:13

我以前也遇到过同样的问题,以下对我有用; 放置一个

while( @ob_end_clean() );

在标头函数之前

header("Content-Type: ". $row['p_mime']);
header("Content-Length: ". $row['p_size']);
header("Content-Disposition: inline; filename=".$row["p_name"]);

内容: Content-disposition:附件/内联必须根据情况进行设置(1.提示下载/2.在浏览器中打开)

注意:请注意不要在标头函数之前回显和值,并且过度谨慎不会造成任何伤害,在您认为会失败的标头函数之前静默所有函数,或者在这些 php 代码行中生成一条警告消息,并在这些行中添加“@”符号。

一切顺利 :)

I had faced the same problem sometime back, following worked for me; put a

while( @ob_end_clean() );

just before header functions:

header("Content-Type: ". $row['p_mime']);
header("Content-Length: ". $row['p_size']);
header("Content-Disposition: inline; filename=".$row["p_name"]);

Content-disposition: attachment/inline has to be set according to cases (1. prompt for download / 2. open in browser)

NOTE: Take care that you are not echoing and value before the header function, and being over cautious will not do any harm, silent out all the function before header function which you think would fail or spawn a warning message prefixing "@" symbol to those lines of php code.

all the best :)

得不到的就毁灭 2024-09-25 07:46:13

确保退出...

(我使用的是 blob)

header("Content-Type: " . $response['content_type'] );
header("Cache-Control:  maxage=1");
header("Pragma: public"); //fixes ie bug
echo trim($_data);
exit();     

Make sure you exit...

(i'm using a blob)

header("Content-Type: " . $response['content_type'] );
header("Cache-Control:  maxage=1");
header("Pragma: public"); //fixes ie bug
echo trim($_data);
exit();     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文