文件存在,但返回“”;系统找不到指定的文件“”;使用UNC路径时


File exists but returns "The system cannot find file specified" when using UNC path

我使用PHP检查文件是否存在,然后获取文件大小。

代码以前运行得很好,但现在我们使用的是UNC路径,我可以检查该路径是否存在file(exists($filename)),但当我尝试运行exec("getsize" . $filename, $out);时,它尝试运行大约一分钟,然后返回"系统找不到指定的文件"。运行该程序的用户目前是管理员,否则我会认为这是权限问题,但我不确定如果它找到了file_exists()的文件,但exec()失败了,还会有什么问题。

任何帮助或要点都将不胜感激,谢谢!

代码示例:

<?php
$filename = "''''server''share''file_path_with_folders''3019-74 (2).zip"; //Example file
if(file_exists($filename)){
    echo "File Exists: " . $filename . "'r'n";  
    // "File Exists: " . $filename" are getting echoed out, so it is succeeding
} else {
    echo "File doesn't exist: " . $filename  . "'r'n";
}
exec("getsize" . $filename, $out); //Runs command line command
//Getting "The system cannot find the file specified" error
echo "Out: " . $out[0] . "'r'n";
//Echos "Out: " and nothing else

?>

如果有疑问,请运行Process Monitor。

不过,说真的,你应该用你最喜欢的文件访问监控实用程序来监控这个过程。

我不得不冒险猜测一下,但文件名中的空格可能会导致您的问题。调用getsize时,可能需要用一组引号将$filename括起来。

在汇编要传递给exec()的命令时,还需要在命令和该命令的参数之间留一个空格。(此脚本生成的结果相当于"getsizetest.txt",而不是"getsize test.txt")

如果可能的话,您应该在本地机器上的交互式PHP shell中运行这些命令。