环境:wampserveer
数据库编码:统一utf8_general_ci
php文件编码:utf-8
该函数不与数据库连接时,下载正常
我用php上传统一图片后再下载就无法打开
我怀疑是上传到mysql后文件编码问题,但从未涉及这一方面,小弟特此求教如何解决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;
}
数据库编码:统一utf8_general_ci
php文件编码:utf-8
该函数不与数据库连接时,下载正常
我用php上传统一图片后再下载就无法打开
我怀疑是上传到mysql后文件编码问题,但从未涉及这一方面,小弟特此求教如何解决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;
}
2、检查所有参与的文件是否有 bom 头
你好,检查$file是否正常,我在连接mysql下,通过echo 可以输出正确带双引号路径
bom头我之前完全没有接触过,不知如何调试?
如果是图片,那么在数据库中是以流的方式存储的。
$file的路径是否指向你要下载的图片?
如果是图片,那么在数据库中是以流的方式存储的。
$file的路径是否指向你要下载的图片?
再就是看看你有没有添加bom头
按你的流程下载一张图片,并保存
然后单独执行以下代码,并贴出结果
$fn = '你保存的文件名';
$s = file_get_contents($fp);
echo base64_encode(substr($s, 0, 20));
$fp = '20120313024414772.jpg';
$s = file_get_contents($fp);
echo base64_encode(substr($s, 0, 20));
?>
输出结果是/9j/4AAQSkZJRgABAQECWAJYAAA=
我刚才不知道为什么下载后可以看到图片了,奇怪。之前不可以的。
再访问数据库时,SET Names utf-8;
所有格式统一;
建议检查图片文件名称,大小,看是不是个正常文件,确定图片没以二进制存在数据库中
图片在服务端是否正常,传输过程是否被加密
如果图片整体内容都被破坏了,那基本就是编码问题另外...图片放数据库里? 凌乱了...