在PHP中,访问文件夹中名称中有空格的文件


Access a file within a folder with spaces in the name in PHP

我想用PHP打开文件夹中的一个文件。问题是包含该文件的文件夹的名称中可能有空格。我用来打开文件的代码(不起作用)如下:

$myFile = "path/to the/file.txt";
$myFile = str_replace(' ', '' ', $myFile);
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;

正如你所看到的,我试图通过在空间前面放置'来解决问题,但这并不能解决问题。我仍然收到以下错误:

Warning: fopen(path/to' the/file.txt) [function.fopen]: failed to open stream: No such file or directory in /path/to/my/website on line 49

你知道如何解决这个问题吗?

谢谢!

Backsplash用于转义命令行上的空格,因为命令行上的空格是参数分隔符。PHP中不存在这样的问题。如果你给fopen任何一个字符串,它期望这个字符串是一个路径。它不会在空格处将路径分解为单独的参数,因为在这种情况下这毫无意义。

因此,不要添加任何反斜杠。路径CCD_ 3与路径path/to the/file.txt不同。