0

帖子移至: 导致 PHP 抛出“无法打开流:没有这样的文件或目录”的网络计时问题

编辑:应该注意的是,这段代码已经到位并且工作了一段时间。我们将服务器配置从具有本地存储图像的单个邮件服务器切换到具有存储图像的共享 NAS 盒的负载平衡配置。然后php开始断断续续地说文件不存在。

99% 的情况下,您会收到一条错误消息,指出文件不存在、文件不存在、存在拼写错误等。

我们有一个将文件上传到临时位置的 .Net 站点。然后我们向传递临时文件的 php 应用程序发送请求。然后,php 应用程序应将文件从其临时位置移动到其永久位置。

我们知道该文件存在,因为在 .Net 中,在将请求发送到 php 应用程序之前,我们会在文件上获取 FileInfo 对象,并检查 FileInfo.Exists,

如果 .Net 确定该文件确实存在,它会将文件路径作为 webrequest 中的参数发送到 php 应用程序。

但是,间歇性地,大约 3 次中的 2 次,php 应用程序给出以下错误:

“无法打开流:没有这样的文件或目录”

事实上,它是间歇性发生的,.Net 说它存在,我们可以导航到磁盘上的位置并进行验证,这表明是的,该文件确实存在。

那么,为什么 php 会抛出此异常的其他一些可能性是什么?

。网:

public void Save(string tempPath, string username, string password, bool deleteSrc, string merakController = "", string fileNamePrefix = "")
        {
            FileInfo fi = new FileInfo(tempPath);
            if (fi.Exists)
            {
                Name = (fileNamePrefix ?? string.Empty) + fi.Name;
                FileSize = fi.Length;
                TimeStamp = fi.LastWriteTimeUtc;
                string path = fi.FullName;

                if (string.IsNullOrEmpty(Type))
                    throw new Exception("Attachment Type not set");

                IceWarpGroupware_Http gwh = new IceWarpGroupware_Http(username, password);
                string parameters = string.Format("AttName={0}&AttType={1}&AttDesc={0}&AttSize={2}&AttTime={3}", Name, Type, FileSize, Time);
                bool result = gwh.AddAttachment(m_oid, path, parameters, string.Empty, merakController);

                if (!result)
                    throw new Exception("Unable to add attachment \"" + Name + "\"");

                try
                {
                    if (deleteSrc)
                        fi.Delete();
                }
                catch { }
            }
            //else
              //  throw new Exception("Temp file not found");
        }

php:

public function AddAttachment($itemID, $src, $parameters, $value)
    {

        //base64 encode necessary parameters.
        $parameters64 = $this->getBase64(urldecode($parameters));
        $value64 = $this->getBase64($value);

        //Per the API documentation, ADD first, then LOCK, then move manually and then UNLOCK.
        $result = $this->FetchComObject()->FunctionCallBase64('AddAttachment', $this->gsid64, $this->getBase64($itemID), $parameters64, $value64);  //Add the attachment.
        $dest = $this->lockAttachment($this->gsid64, $this->getBase64($itemID), $result);  //Lock the attachment and get the destination path.

        //This is an enhanced version of the file copy routine for error handling if necessary.  Please let alone and commented out when not in use.
        // if (!copy($src, $dest))
        // {
            // if (isset($GLOBALS['php_errormsg']) && !empty($GLOBALS['php_errormsg']))
                // throw new RuntimeException($GLOBALS['php_errormsg']);
            // throw new RuntimeException("File copy failed with an undefined error. [".$src."]");
        // }

        $copySuccess = copy($src, $dest);  //Comment this line out if using the enhanced file copy above.
        //unlink($src);

        $success = $this->unlockAttachment($this->gsid64, $this->getBase64($itemID), $result);  //Unlock the attachment.
        if ($copySuccess == true)
            return true;
        return false;
    }
4

0 回答 0