Heim  >  Artikel  >  Backend-Entwicklung  >  php 文件下载 出现下载文件内容乱码损坏的解决办法

php 文件下载 出现下载文件内容乱码损坏的解决办法

WBOY
WBOYOriginal
2016-06-13 12:29:16957Durchsuche

php 文件下载 出现下载文件内容乱码损坏的解决方法

在做PHP程序时,需要使用PHP做的文件下载

那做就做呗把代码复制过来改了一改.

很多效果也出来了

那么点击下载后,恩,本以为搞定了文件下载,谁知道,图片打开的时候提示文件损坏,这我就观察了文件的大小.似乎真是多了 1KB左右.

使用PS打开,恩,提示了损坏,不过图象可以出来.

那么我就认为是 那多出来的 1kb的问题了.

然后,我把一个txt上传上去,然后下载.

果然发现,文件的前面以及后面,都多了几个 字符.

那我就在想应该怎么去掉啊.各种百度,谷歌,上论坛提问.

最后,经过我一个多小时的搜索,终于找到了解决办法.

代码如下

<code>public function download(){    $upload=M('upload');    $data=$upload->find($_GET['id']);    if(empty($data)){        header('HTTP/1.0 404 Not Found');        header('Location: .');    }else{    $path='./public/Uploads/'.$data['savename'];    header("Content-Type:".$data['type']);    header('Content-Disposition: attachment; filename="'.$data['name'].'"');    header('Content-Length:'.$data['size']);    ob_clean();    flush();    readfile($path);    }}</code>

注意:

<code>ob_clean();flush();</code>

这个是关键哦.用了这两函数,搞定,图片正常打开,文字也不乱码.


转载自:http://www.9958.pw/post/download_content

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn