title: PHP move_uploaded_file() returning FALSE
body: <p>I've been struggling with this for more than 8h now, so I hope someone can help me. I think I went through all the posts regarding this issue, but wasn't able to find a solution.</p>
  
  <p>There are no errors, both <code>/tmp</code> and <code>/var/www/test</code> are owned by apache (<code>www-data</code>) and have 775 permission set.</p>
  
  <p>This is the PHP code:</p>
  
  <pre><code>$target_dir = $_SERVER["DOCUMENT_ROOT"] . "test/uploads/";
  $target_file = $target_dir . basename($_FILES["pic"]["name"]);
  $uploadOK = 1;
  $image_file_type = pathinfo($target_file, PATHINFO_EXTENSION);
  
  if(isset($_POST['submit'])) {
  if($uploadOK === 0) {
      echo "Upload failed!";
  } else {
      // This is where it stops...
      // There is definitely something wrong with $target_file
      if(move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file)) {
          echo "Success";
      } else {
          echo "Not uploaded...&lt;br&gt;";
          echo move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file) . "&lt;br&gt;";
          echo $target_file . "&lt;br&gt;";
          print_r($_FILES) . "&lt;br&gt;";
          error_reporting(E_ALL);
      }
  }
  </code></pre>
  
  <p>These are the results I get:</p>
  
  <p>Not uploaded...</p>
  
  <p>/var/www/test/uploads/test.jpg</p>
  
  <pre><code>Array (
      [pic] =&gt; Array (
          [name] =&gt; test.jpg
          [type] =&gt; image/jpeg
          [tmp_name] =&gt; /tmp/phpKlzCxc
          [error] =&gt; 0
          [size] =&gt; 721090
      )
  )
  </code></pre>
  
  <p>So, what am I doing wrong?</p>
  
  <p><strong>Ok, here are the updates:</strong></p>
  
  <ul>
  <li>The checking of the upload was done at the beginning of the script and it worked which is why I didn't include it in the question.</li>
  <li>This is the code from www.w3schools.com, and I will be checking out the other source (thanks for pointing that out)</li>
  <li>After fixing the position of the <code>error_reporting</code> (good call :)), finally got a whole bunch of "Permission denied" warnings as I suspected would be the problem, and here they are:</li>
  </ul>
  
  <p><strong>Warning:</strong> move_uploaded_file(/var/www/test/uploads/test.jpg): failed to open stream: Permission denied in /var/www/test/upload.php on line 47</p>
  
  <p><strong>Warning:</strong> move_uploaded_file(): Unable to move '/tmp/phpHzvhOA' to '/var/www/test/uploads/test.jpg' in /var/www/test/upload.php on line 47</p>
  
  <p>Line 47:</p>
  
  <pre><code>if(move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file)) {
  </code></pre>
  
  <p>Permission set for /var/www/test/upload.php:</p>
  
  <pre><code>-rwxrwxr-x 1 www-data www-data 2174 Sep  9 00:07 /var/www/test/upload.php
  </code></pre>