问题说明:今天再做附件下载时,因为附件名称为中文,在苹果手机safari浏览器下载文件中文名称直接乱码,如图:
解决方法:
php代码:
public fuction downloadFile($fileurl, $filename, $charset = 'UTF-8'){ if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])) { $filename = urlencode($filename); $filename = str_replace("+", "%20", $filename);// 替换空格 $attachment = "attachment; filename=\"{$filename}"; charset={$charset}"; } else if (preg_match("/Firefox/", $_SERVER["HTTP_USER_AGENT"])) { $attachment = 'attachment; filename*=utf-8\'\'' . $filename; } else if (preg_match("/Safari/", $_SERVER["HTTP_USER_AGENT"])) { $filename = rawurlencode($filename); // 注意:rawurlencode与urlencode的区别 $attachment = 'attachment; filename*=utf-8\'\'' . $filename; } else { $filename = rawurlencode($filename); $attachment = "attachment; filename=\"{$filename}"; charset={$charset}"; } header('Pragma: public'); header("Content-Transfer-Encoding: Binary"); header('Content-Type: application/octet-stream'); header("Content-disposition:".$attachment.""); readfile($fileurl); }