Home > Backend Development > PHP Tutorial > php下载图片到本地,图片打不开,该怎么处理

php下载图片到本地,图片打不开,该怎么处理

WBOY
Release: 2016-06-13 10:12:21
Original
1827 people have browsed it

php下载图片到本地,图片打不开
环境:wampserveer
数据库编码:统一utf8_general_ci
php文件编码:utf-8
该函数不与数据库连接时,下载正常
我用php上传统一图片后再下载就无法打开
我怀疑是上传到mysql后文件编码问题,但从未涉及这一方面,小弟特此求教如何解决

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->function dl_file($file){     //这是下载文件的函数,$file是文件路径.    //First, see if the file exists    if (!is_file($file)) { die("<b>404 File not found!</b>"); }    //Gather relevent info about file    $len = filesize($file);    $filename = basename($file);    $file_extension = strtolower(substr(strrchr($filename,"."),1));    //This will set the Content-Type to the appropriate setting for the file    switch( $file_extension ) {          case "pdf": $ctype="application/pdf"; break;      case "exe": $ctype="application/octet-stream"; break;      case "zip": $ctype="application/zip"; break;      case "doc": $ctype="application/msword"; break;      case "xls": $ctype="application/vnd.ms-excel"; break;      case "ppt": $ctype="application/vnd.ms-powerpoint"; break;      case "gif": $ctype="image/gif"; break;      case "png": $ctype="image/png"; break;      case "jpeg":      case "jpg": $ctype="image/jpg"; break;      case "mp3": $ctype="audio/mpeg"; break;      case "wav": $ctype="audio/x-wav"; break;      case "mpeg":      case "mpg":      case "mpe": $ctype="video/mpeg"; break;      case "mov": $ctype="video/quicktime"; break;      case "avi": $ctype="video/x-msvideo"; break;      //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)      case "php":      case "htm":      case "html":      case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;      default: $ctype="application/force-download";    }    //Begin writing headers    header("Pragma: public");    header("Expires: 0");    header("Cache-Control: must-rev alidate, post-check=0, pre-check=0");    header("Cache-Control: public");     header("Content-Description: File Transfer");        //Use the switch-generated Content-Type    header("Content-Type: $ctype");    //Force the download    $header="Content-Disposition: attachment; filename=".$filename.";";    header($header);    header("Content-Transfer-Encoding: binary");    header("Content-Length: ".$len);    @readfile($file);    exit;}
Copy after login



------解决方案--------------------
1、检查传入参数 $file 内容是否正常
2、检查所有参与的文件是否有 bom 头
------解决方案--------------------
获取扩展名,建议用pathinfo

------解决方案--------------------
探讨
我存放的是图片路径,路径我有echo过,没有问题的,bom头如何查看的?

------解决方案--------------------
一定是数据库编码的问题,因为你都说了不用数据库时正常,
再访问数据库时,SET Names utf-8;
所有格式统一;
建议检查图片文件名称,大小,看是不是个正常文件,确定图片没以二进制存在数据库中
图片在服务端是否正常,传输过程是否被加密
------解决方案--------------------
首先对照一下异常的图片和正常的图片,有什么区别,然后再寻找异常原因才是正解吧...

如果异常图片只是开头或结尾被破坏,那就是php代码本身写的有问题,哪里截断或添加了异常数据;
如果图片整体内容都被破坏了,那基本就是编码问题

另外...图片放数据库里? 凌乱了...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template