PHP move_uploaded_file not working, yet no errors generated
<p>I have a file upload page, and I have gone through the code and inserted debugging lines incrementally, line-by-line, to try to find the cause of this problem. The problem occurs somewhere in this block of code:</p>

<pre><code>      $tmp_name = $_FILES['image_large']['tmp_name'];
      chdir('..'.DIRECTORY_SEPARATOR.'photos');
      $name = getcwd().DIRECTORY_SEPARATOR.$_FILES['image_large']['name'];        
      $success = move_uploaded_file($tmp_name,$name);
      $errormsg = "&lt;p&gt;source: {$tmp_name}&lt;/p&gt;&lt;p&gt;destination: {$name}&lt;/p&gt;";
      $errormsg .= "&lt;p&gt;error: {$success}&lt;/p&gt;";
</code></pre>

<p>This <code>$errormsg</code> gets printed on the page, and displays the following result:</p>

<blockquote>
  <p>source: C:\xampp\tmp\php4A14.tmp</p>
  
  <p>destination: C:\xampp\htdocs\photos\fingerlakes1.jpg</p>
  
  <p>error: 1</p>
</blockquote>

<p>EDIT: The above is really confusing, so let me clarify. The "error: 1" does NOT refer to $_FILES['image_large']['error']. It's a boolean referencing whether or not move_uploaded_file worked, so getting 1 is the desired result for that.</p>

<p>That destination is the correct, intended location for the file to go. Yet when I check, there is no file "C:\xampp\htdocs\photos\fingerlakes1.jpg" produced, even though the debugging results seem to suggest the computer is convinced it performed the task successfully.</p>

<p>I have also made sure the folder has write permissions enabled (I'm on Windows 7), although I think that if that were the cause, <code>$success</code> would not have evaluated as true.</p>

<p>EDIT: I also tried using <code>print_r($_FILES)</code> to check everything, and it returned:</p>

<blockquote>
  <p>Array ( [image_large] => Array ( [name] => fingerlakes1.jpg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\php5694.tmp [error] => 0 [size] => 497376 ) [image_thumb] => Array ( [name] => fingerlakes1.jpg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\php5695.tmp [error] => 0 [size] => 26228 ) ) </p>
</blockquote>

<p>So it appears that the files are uploaded correctly to the tmp folder, at least.</p>

<p>EDIT 2: Further information in case it helps: I am using XAMPP, with the XAMPP Control Panel running in Administrator Mode. I am only local-testing right now (in other words, the file upload should merely copy the file from one part of my computer to another). I went into the destination folders "photos" and "thumbnails" and did right-click -> properties -> Security, and made sure that every single user listed  has write access, just to make sure. There is only one account on this computer, but it listed the account name twice as user and administrator, plus SYSTEM and one other one I forget. But I gave them all write access to be on the safe side.</p>

<p>EDIT 3: Per request, I tried replacing some code in the first code box at the top of this post, to test the problem, replacing the chdir and getcwd lines with giving a literal path+filename for the destination. The code currently looks like this:</p>

<pre><code>      $tmp_name = $_FILES['image_large']['tmp_name'];
      //chdir('..'.DIRECTORY_SEPARATOR.'photos');
      //$name = getcwd().DIRECTORY_SEPARATOR.$_FILES['image_large']['name'];
      $errormsg .= "&lt;p&gt;source: {$tmp_name}&lt;/p&gt;";
      $errormsg .= "&lt;p&gt;destination: C:\\xampp\\htdocs\\photos\\fingerlakes1.jpg&lt;/p&gt;";
      $success = move_uploaded_file($tmp_name,"C:\\xampp\\htdocs\\photos\\fingerlakes1.jpg");
</code></pre>

<p>I had to use escape sequences for the backslashes writing out the destination string. I forgot to do that at first, and printed errors on the page showing the destination string being messed up. I'm assuming that it didn't have a problem with the backslashes embedded in the variables $tmp_name and $name before, as it didn't give the errors in that case.</p>

<p>I am wondering if perhaps there is a problem with the fact that Windows uses backslashes for directory separators and the internet uses forward-slashes, so... maybe XAMPP is expecting forward slashes? I doubt this is the problem, since lots of people use XAMPP locally to test their code.</p>

<p>EDIT 4: Possible cause of the problem found: I have discovered that every single folder on my computer has the checkbox ticked for "Read-only". This has not stopped me from doing anything on the computer aside from this php problem, but I googled it anyway. Found this:
<a href="http://itexpertvoice.com/home/fixing-the-windows-7-read-only-folder-blues/" rel="nofollow">http://itexpertvoice.com/home/fixing-the-windows-7-read-only-folder-blues/</a></p>

<p>So it seems it's a Windows bug, but Windows usually ignores read-only settings anyway. However, I wonder if the issue with move_uploaded_file might be related to this. I will try some of the solutions on that page and report back.</p>

<p>EDIT 5: I am starting to strongly suspect that there is not actually anything wrong with the code and that this is more of a Windows issue. One thing I neglected to mention is that I originally had all the site files on another hard drive, with XAMPP installed, and the file upload worked there, and the code hasn't changed since moving to a new drive. The only thing different is that, back then, I had manually dropped the images into the destination folder, and then used the file upload (essentially uploading them to overwrite themselves) so that I could use the rest of the file upload form and write to the MySQL database. That worked. One of the reasons that I discovered the current problem is that the code to write to the databases is getting blocked by the problem with the file upload.</p>

<p>So I'm going to try a couple of other things. The first, is to test doing exactly what I did before... putting the file in the place I want it and then using the upload form to overwrite the file, and checking to see if it then was able to process the rest of the php code to write to the database.</p>

<p>The second thing I am going to check is to upload the database to the web host, along with all the php/html files needed to run the administrative side of the site. I have a nagging feeling that the file upload form is going to work just fine even though it won't work locally on my computer.</p>

<p>I'll try these when I get home from work and report back.</p>
