【问题标题】:File exists but returns "The system cannot find file specified" when using UNC path文件存在但使用 UNC 路径时返回“系统找不到指定的文件”
【发布时间】:2015-03-26 18:15:03
【问题描述】:

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


?>

【问题讨论】:

    标签: command-line php unc


    【解决方案1】:

    如有疑问,请运行Process Monitor.

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

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

    在组合命令以传递给 exec() 时,您还需要在命令和该命令的参数之间留一个空格。 (这个脚本产生了“getsizetest.txt”的等价物,而不是“getsize test.txt”)

    如果可能的话,您应该真正在本地计算机上的 interactive PHP shell 中运行这些命令。

    【讨论】:

    • 我以为你有引号,但我用双引号括起来 $filename 并没有帮助。我不确定我能否在服务器上运行它,必须与服务器人员交谈,但我一定会记住这一点。谢谢@EKW!
    • @gv0000 添加了一条关于我今天早上注意到的另一个错误的注释,我强烈建议使用 PHP 交互式 shell(这就是让我注意到另一个错误的原因......如果我遵循我自己的建议从...开始)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多