command-line - 使用 UNC 路径时文件存在但返回 "The system cannot find file specified"

标签 command-line php unc

我正在使用 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/

相关文章:

linux - "vino-preferences"在哪里存储其配置属性

command-line - redis-cli 的 Vi 模式

php - Codeigniter 4 验证可选值

c++ - 限制程序对 unc 服务器的访问

c# - 物理、相对、绝对和其他路径

安装了Python3.6但版本显示为3.5.2

从命令行管理命令运行时,Django 日期本地化似乎不起作用

php - 自动重定向到干净的网址

php - 使用 php/Mysql 中的选择查询将日期/时间从 24 小时制转换为 12 小时制 AM/PM

c++ - 如何为 UNC 路径制作长 Unicode 版本?