Note that there are some explanatory texts on larger screens.

plurals
  1. POmove_uploaded_file strange behaviour that leads to a bug
    text
    copied!<p>I do have a very strange behaviour while trying to upload a file via a PHP script.</p> <p>First of all, here are a few infos:</p> <ul> <li>I do have the enctype set to "<code>multipart/form-data</code>" in my form</li> <li>Upload file size is set to 20M in apache config</li> <li><p>My upload directory <code>/home/www/public/files/images</code> have a 777 chmod</p> <pre><code>ls -l /home/www/public/files/ total 68K drwxrwxrwx. 90 pel pel 64K juin 23 18:06 images </code></pre></li> </ul> <p>Now, here is the code I use:</p> <pre><code> if (!empty($_FILES['image'])) { $dir = '/home/www/public/files/images'; $extension = mb_strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION)); $newfile_name = 'myimage-with-my-name.' . $extension; $newfile_path = $dir . '/' . $newfile_name; if (!is_writable($dir)) { throw new Exception('Upload: Cant write in ' . myescape($dir)); } if (!in_array($extension, array('png', 'gif', 'jpg', 'jpeg'))) { throw new Exception('Upload: Unsupported extension ' . myescape($extension)); } if ($_FILES['image']['error'] &gt; 0) { throw new Exception('Upload: Error code ' . myescape($_FILES['image']['error'])); } print_r($_FILES['image']); print_r(array($_FILES['image']['tmp_name'], $newfile_path)); if (!move_uploaded_file($_FILES['image']['tmp_name'], $newfile_path)) { die('ERROR: Upload Failed'); } } </code></pre> <p>And here is the result:</p> <pre><code> Array ( [name] =&gt; sampleimage.jpg [type] =&gt; image/jpeg [tmp_name] =&gt; /tmp/phpaLjZyD [error] =&gt; 0 [size] =&gt; 11721 ) Array ( [0] =&gt; /tmp/phpaLjZyD [1] =&gt; /home/www/public/files/images/myimage-with-my-name.jpg ) Error: Upload Failed </code></pre> <p>As you can see, there is no error, no "previous" exception is thrown (is_write, extension ...), and finally the <code>move_uploaded_file</code> fails.</p> <p>The strange behaviour is:</p> <ul> <li>The move_uploaded_file returns false</li> <li>The image is nevertheless uploaded with the name <code>sampleimage.jpg</code> and not <code>myimage-with-my-name.jpg</code> as I wanted</li> <li>No error in the apache2 logs. I watched with tail -f during every tests</li> </ul> <p>Do you have any tips or a solution to solve this problem?</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload