php文件重命名下载代码示例
Bata1.0、基础代码(file_exists仅支持本地,还有下载时文件名乱码问题)
<?php
$file = 'success.jpg';
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;
}
?>
Bata2.0、解决中文名显示乱码问题(但还是仅支持服务器本地)
//$down_url=$server_name.$renamefile_url.'/'.$renamefile_name; //file_exists仅支持本地
$down_url=$renamefile_url.'/'.$renamefile_name;
//$sourcefile_name = iconv("UTF-8",'GBK',$sourcefile_name);
$sourcefile_name = urlencode($sourcefile_name);
$sourcefile_name = str_replace("+", "%20", $sourcefile_name);
if(file_exists($down_url)){
$ua = $_SERVER["HTTP_USER_AGENT"];
//header('Content-Disposition: attachment; filename='.basename($sourcefile_name));
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $sourcefile_name . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $sourcefile_name . '"');
} else {
}
header('Content-Length: ' . filesize($down_url));
readfile($down_url);
else{
echo '文件目录不存在';
本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/1097932,如需转载请自行联系原作者