我正在使用 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”)
您确实应该在 interactive PHP shell 中运行这些命令如果可能的话,在您的本地计算机上。
关于command-line - 使用 UNC 路径时文件存在但返回 "The system cannot find file specified",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29352768/