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

WBOY
Release: 2016-06-23 13:23:01
Original
921 people have browsed it

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');
Copy after login

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

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

转码之后一切正常

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