【问题标题】:how to read the content of text files having white space between file name?如何读取文件名之间有空格的文本文件的内容?
【发布时间】:2013-03-08 16:58:09
【问题描述】:

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

但是当我尝试将包含空格的文件读取到文件名中时,问题就开始了。

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

是否有其他方法可以读取 .txt 文件?

【问题讨论】:

  • 尝试用“\”替换空格
  • 我也试过了..但没有得到..

标签: php text file-get-contents readfile


【解决方案1】:

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

【讨论】:

  • 我使用的是linux系统,我通过了url $filenames="/home/project/news4u/web/".urlencode('ede ede10696.txt'); $content = file_get_contents($filenames);回声$内容;结果是空的。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2023-02-02
      • 2020-09-29
      相关资源
      最近更新 更多