如何读取文件名之间有空格的文本文件的内容?

时间:2012-07-04 06:47:48

标签: php text file-get-contents readfile

我想在文本文件中获取内容,而我正在使用file_get_contents()

但是当我尝试将具有空格的文件读入文件名时,问题就开始了。

例如:使用/home/project/text cv.txt无法读取file_get_contents()

是否有读取.txt文件的替代方法?

3 个答案:

答案 0 :(得分:3)

文档中有一个提示: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

答案 1 :(得分:2)

另一种选择是使用realpath()将URI包装到您的文件中:

$path = realpath( dirname(__FILE__).'/../_files/file with space.txt' );
$data = file_get_contents($path);

答案 2 :(得分:0)

尝试file_get_contents(text cv);file_get_contents(text%20cv);