Possible reasons for "failed to open stream: No such file or directory" when file does exists
<p><strong>Post Moved to:</strong> <a href="http://stackoverflow.com/questions/10502772/network-timing-issues-causing-php-to-throw-failed-to-open-stream-no-such-file">Network timing issues causing php to throw &quot;failed to open stream: No such file or directory&quot;</a></p>

<p><strong>edit</strong>: It should be noted that this code was already in place and working for some time. We switch our server configuration from a single mail server with the images stored locally, to a load balanced configuration with a shared NAS box where the images are stored. Then php started to intermittently say the file doesn't exists.</p>

<p>99% of the time when you get an error saying the file doesn't exist, the file doesn't exist, there is a typo or some such. </p>

<p>We have a .Net site which uploads a file to a temporary location. We then send a request to a php application passing in the temporary file. The php app should then move the file from its temporary location to its permanent location.</p>

<p>We know the file exists, because, in .Net, before sending the request to the php application, we are getting a FileInfo object on the file, and checking FileInfo.Exists,</p>

<p>if .Net determines that the file does exist, it sends the filepath as a parameter in a webrequest to the php application. </p>

<p>However, intermittently, about 2 out of 3 times, the php application gives the following error:</p>

<p>"failed to open stream: No such file or directory"</p>

<p>The fact that it is happening intermittently, .Net says it exists, and we can navigate to the location on disk and verify, indicates that yes, the file does actually exists. </p>

<p>So, what are some other possibilites for why php would throw this exception?</p>

<p>.Net:</p>

<pre><code>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}&amp;AttType={1}&amp;AttDesc={0}&amp;AttSize={2}&amp;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");
        }
</code></pre>

<p>php:</p>

<pre><code>public function AddAttachment($itemID, $src, $parameters, $value)
    {

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

        //Per the API documentation, ADD first, then LOCK, then move manually and then UNLOCK.
        $result = $this-&gt;FetchComObject()-&gt;FunctionCallBase64('AddAttachment', $this-&gt;gsid64, $this-&gt;getBase64($itemID), $parameters64, $value64);  //Add the attachment.
        $dest = $this-&gt;lockAttachment($this-&gt;gsid64, $this-&gt;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']) &amp;&amp; !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-&gt;unlockAttachment($this-&gt;gsid64, $this-&gt;getBase64($itemID), $result);  //Unlock the attachment.
        if ($copySuccess == true)
            return true;
        return false;
    }
</code></pre>
