我在本地服务器上编写了一个PHP脚本,该脚本需要包含文件的路径,我需要在其中使用file_put_contents将内容写入文件,该文件在localhost上没有任何问题。 我将文件移动到已配置了路径的Web服务器,该路径之间有空格
例如:
C: Program File(xxx) Apache Software Foundation Apache2.2 htdocs writecode writefile
当我尝试打开文件夹writefile将内容写入该文件夹中的temp_1.cfg文件时,它弹出错误
Warning:
file_put_contents(C:\ProgramFile(xxx)\ApacheSoftwareFoundation\Apache2.2\htdocs\writtencode\writefile\temp_1.cgf)
[function.file-put-contents]: failed to open stream: No such file or
directory in C:\Program File (xxx)\Apache Software
Foundation\Apache2.2\htdocs\writtencode\writefile\index.php
使用file_put_contents将内容写入文件时,如何删除或解决路径中的空格的想法?
$path ="C:\ProgramFile(xxx)\Apache Software Foundation\Apache2.2\htdocs\writtencode\writefile\temp_1.cgf";
file_put_contents($path = str_replace(' ', '\ ', $path));
您是否尝试将路径用引号引起来?
另外,您可能需要转义所有反斜杠,这样:
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);