【问题标题】:How to deal with whitespace in the path of the root folder如何处理根文件夹路径中的空格
【发布时间】:2011-11-30 10:23:32
【问题描述】:

我在我的本地服务器上编写了一个 PHP 脚本,它需要包含我需要使用 file_put_contents 将内容写入文件的文件路径,这在本地主机上没有任何问题。我将文件移动到了配置了路径的网络服务器,路径之间有空格 例如: C:\Program File (xxx)\Apache Software Foundation\Apache2.2\htdocs\writtencode\writefile\

当我尝试打开文件夹 writefile 以将内容写入该文件夹中的 temp_1.cfg 文件时,它会弹出错误

警告: file_put_contents(C:\ProgramFile(xxx)\ApacheSoftwareFoundation\Apache2.2\htdocs\writtencode\writefile\temp_1.cgf) [function.file-put-contents]:无法打开流:没有这样的文件或 C:\Program File (xxx)\Apache Software 中的目录 Foundation\Apache2.2\htdocs\writtencode\writefile\index.php

知道如何在使用file_put_contents 将内容写入文件时删除或解决路径中的空格问题吗?

【问题讨论】:

    标签: php whitespace


    【解决方案1】:
    $path ="C:\ProgramFile(xxx)\Apache Software Foundation\Apache2.2\htdocs\writtencode\writefile\temp_1.cgf";
    file_put_contents($path = str_replace(' ', '\ ', $path));
    

    【讨论】:

      【解决方案2】:

      您是否尝试用引号将路径括起来? 此外,您可能需要转义所有反斜杠:

      file_put_content(C:\ProgramFile(xxx)\ApacheSoftwareFoundation\Apache2.2\htdocs\writtencode\writefile\temp_1.cgf);

      变成:

      file_put_content('C:\\ProgramFile(xxx)\\ApacheSoftwareFoundation\\Apache2.2\\htdocs\\writtencode\\writefile\\temp_1.cgf');

      【讨论】:

      • 我已将路径用引号括起来,我需要在路径中包含空格,以便它获取空格并将其写入正确的文件
      • 好的,为了一个简单的修复,我会添加:subst x: "C:\Program File (xxx)\Apache Software Foundation\Apache2.2\htdocs\" 到批处理文件(例如 mapx.bat)。运行这个批处理文件,现在可以通过驱动器 x 访问您的 htdocs 文件夹:file_put_content('x:\writtencode\writefile\temp_1.cgf');
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      相关资源
      最近更新 更多