Home  >  Article  >  Backend Development  >  file_get_contents读取路径中包含中文文件名有关问题

file_get_contents读取路径中包含中文文件名有关问题

WBOY
WBOYOriginal
2016-06-13 13:15:451236browse

file_get_contents读取路径中包含中文文件名问题
$data = file_get_contents($file_names);中$file_names中指向的路径中包含中文文件名称就报错[function.file-get-contents]: failed to open stream: No such file or directory不用中文的文件名就可以,纠结啊!不能重命名文件的

------解决方案--------------------
$file_names = "D:/我的文档/Youku Files\download/[LIVE]汪峰.-.春天里.flv";
$s = file_get_contents($file_names);
echo strlen($s);//7216760

不存在你说的问题,当然你要正确的书写文件名(包括语言编码)
------解决方案--------------------
关键在于你的 $file_names 中的文件名的编码要与操作系统的语言编码一致
------解决方案--------------------

探讨

关键在于你的 $file_names 中的文件名的编码要与操作系统的语言编码一致

------解决方案--------------------
中文的时候看下echo strlen($data);有没有取到内容
------解决方案--------------------
iconv("utf-8", "gbk",file_get_contents('url'));
先换成操作系统的编码试试能打开文件内容吗?你这主要是中文路径打不开文件,获取不到内容所以无法替换吧
------解决方案--------------------
1.是不是文件名中有空格存在。
2.再就是编码问题,将文件目录打印出来看检查一下。
------解决方案--------------------
你的文件名是用目录函数读取的,不需要做编码转换
为什么不用 glob 函数呢? 
scandir 读取的文件名不含路径,而 glob 读取的文件名是含路径的。可以免去组装文件名的麻烦
读取后缀可用 pathinfo($filename, PATHINFO_EXTENSION) 简单且高效

另外请告知你的php版本号,低版本的有可能中文文件名无效。确认一下
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