3

I'm trying to upload a file (msword/doc) to a Apache server folder via an HTML form. It works when I test it locally (I'm testing it via MAMP), but when I upload it to a remote server (such as GoDaddy), it doesn't work. It shows "There was a problem with the file upload".

Below is the snippet of code that processes the file upload. I can't figure out what is wrong with my conditional.

      // Move the file to the target upload folder
      $target = FILE_UPLOADPATH . basename($new_file);
      if (move_uploaded_file($_FILES['new_file']['tmp_name'], $target)) 
      {
        // The new file move was successful, now make sure any old file is deleted
        if (!empty($old_file) && ($old_file != $new_file)) 
        {
          @unlink(FILE_UPLOADPATH . $old_file);
        }
      }
      else 
      {
        // The new file move failed, so delete the temporary file and set the error flag
        @unlink($_FILES['new_file']['tmp_name']);
        echo 'There was a problem with the file upload.' . PHP_EOL;
      }
Dharman
  • 30,962
  • 25
  • 85
  • 135
kthaker
  • 31
  • 1
  • 2
  • Can you share the full $_FILES array, as the value in the error param may help shed some light on this – fullybaked Apr 29 '13 at 16:05
  • Here is the $_FILES array output: Array ( [new_file] => Array ( [name] => sample.doc [type] => msword/doc [tmp_name] => /tmp/phpsTu20P [error] => 0 [size] => 119674 )) Thoughts? – kthaker Apr 29 '13 at 16:25
  • The value in the error parameter is 0. – kthaker Apr 29 '13 at 16:28
  • Hmm, error 0 means success. – fullybaked Apr 29 '13 at 16:28
  • Comment out your @unlink call for now to ensure that the only thing tested is your upload, and post results. Does the newly uploaded file still not appear in the directory – fullybaked Apr 29 '13 at 16:29
  • @kthaker What does `var_dump(is_writable($target));` give you? – popthestack Apr 29 '13 at 16:36
  • The issue was with permissions. I didn't realize the server I was testing on had ACLs set up. So my chmod permissions changes didn't go through. Thanks for the advice :) – kthaker Apr 29 '13 at 16:49

4 Answers4

0

If $_FILES['new_file']['error'] == 0 then the upload isn't the problem, but the call to move_uploaded_file() is. You probably have incorrect permissions on the directory you're trying to move the file to.

popthestack
  • 496
  • 3
  • 7
0

Are you sure that the folder you are uploading to has permission for files to be written to it? If not, use chmod 0777 and test with that.

virtuexru
  • 858
  • 1
  • 6
  • 16
0

Does your destination folder have proper permissions? http://en.wikipedia.org/wiki/Chmod The directory write to typically needs 775: What are the proper permissions for an upload folder with PHP/Apache?

Also, do you want users to have direct access to the file? If not you should consider writing the file to a folder that is above your web root directory.

Community
  • 1
  • 1
vcardillo
  • 1,646
  • 3
  • 24
  • 29
  • I double checked the permissions. They are 775. And yes, I do wants users to have direct access to the file. I think the problem may be in the 'move_uploaded_file' line. The database stores the filename in the table and shows it on the form after the upload. But when I check the upload folder, the file is not present. – kthaker Apr 29 '13 at 16:27
0

For me, I happened to be testing locally with a file under php's upload_max_filesize while testing remotely with a file over upload_max_filesize. See https://stackoverflow.com/a/30359278/3325776 for more info.

user3325776
  • 105
  • 2
  • 10