Php下载脚本输出损坏的文件 [英] Php download script outputs corrupted file
问题描述
以png格式的一个示例图像文件我使用脚本下载的图像文件似乎在使用notepad ++更改Encoding后起作用UTF到ASCII
这些是我到目前为止采取的步骤:
<$ c
header('Expires:0');
头('缓存控制:
header('Cache-Control:private',false);
header('Content-Type:'。$ mime );
header('Content-Disposition:attachment; filename ='。basename($ file)。''');
header('Content-Transfer-Encoding:binary');
header('Transfer-Encoding:binary');
header('Content-Description:File Transfer');
header('Content-Encoding:chunked');
header 'connection:closed');
@readfile($ this-> file);
我发现了:
$ file = fopen($ this-> file,'r');
$ read = fread($ file,filesize($ this-> file) );
print($ read);
我做了file_get_contents();
$ read = file_get_contents($ this-> file);
print($ read);
所有这些步骤都将文件发送到下载对话框。但它不会输出文件。而这会影响我尝试使用脚本下载的任何文件。
我做错了什么?
尝试运行一个 ob_start()
作为脚本中的第一件事,写东西,编辑头文件,然后只要您希望将内容发送到用户的浏览器,ob_flush()
和 ob_clean()
这是适用于我..
if(file_exists($ file)){
header ('内容描述:文件传输');
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);
退出;
}
希望这有帮助..
I was building a file download Class for my CMS in PHP, at a time I noticed it outputs files in a different Encoding format. I tried with readfile, file_get_contents,fread but all seem to be doing the same thing. It"s like something that has to do with output buffering.
An example image file in png format I downloaded using the script seems to work after changing the Encoding using notepad++ from UTF to ASCII
These are the steps i've taken so far:
$mime = http::get_file_mime();
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Transfer-Encoding: binary');
header('Content-Description: File Transfer');
header('Content-Encoding: chunked');
header('Connection: closed');
@readfile($this->file);
I did fread:
$file = fopen($this->file,'r');
$read = fread($file,filesize($this->file));
print($read);
I did file_get_contents();
$read = file_get_contents($this->file);
print($read);
All of these steps sends the file to the download dialog. But it doesnt output the file as it is. And this affects any file i try to download with the script.
What am i doing wrong ?
Try running an ob_start()
as the first thing in your script, write stuff, edit your headers and then ob_flush()
and ob_clean()
whenever you want your content to be sent to the user's browser.
This is worked for me..
if (file_exists($file)) {
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;
}
Hope this helps..
这篇关于Php下载脚本输出损坏的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!