Home  >  Article  >  Backend Development  >  php文件下载时遇到中文文件名的处理方法

php文件下载时遇到中文文件名的处理方法

WBOY
WBOYOriginal
2016-06-23 13:23:01882browse

php做文件下载主要用到一下几行代码

header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="/tmp/abc.pdf"'); header('Content-Length: '.filesize('/tmp/abc.pdf'));header('Content-Transfer-Encoding: binary'); readfile('/tmp/abc.pdf');

需要注意的是,如果文件名中有中文,filesize(),file_exists(),readfile()都不会反回预期结果,文件会下载,只是下载的文件大小是0,此时在将文件路径传给以上几个函数前,需要对路径中的中文进行转码:

$filename=iconv('UTF-8','GB2312',$filename);

转码之后一切正常

Statement:
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