0
votes

I'm in the process of moving my sites from a shared hosting to a VPS. When testing on my VPS I noticed that all of the sudden I couldn't upload files through a webform anymore.

The error: Warning: move_uploaded_file(/uploads/logoklein.jpg): failed to open stream: No such file or directory in /var/www/vhosts/denkproducties.nl/httpdocs/denkproducties/upload.php on line 26 Warning: move_uploaded_file(): Unable to move '/tmp/php01nhmx' to '/uploads/logoklein.jpg' in /var/www/vhosts/denkproducties.nl/httpdocs/denkproducties/upload.php on line 26

There are tons of questions about this on SO, and they almost all involve setting the permissions right. I chmodded both the TMP and the uploads directory to 0777 (shudder), to no avail. I know 0777 isn't a smart thing to do, but I wanted to make sure it's not a permission issue for these folders.

Since the only thing that has changed is the VPS, I figured the problem must be on the server. I thought maybe the web server was running as 'nobody', so it couldn't access the tmp folder. I ran:

ps aux | grpe httpd

Which shows me:

root   27371   0.0  2.3   340860   24726  ?   SNs   12:57   0:00   /usr/sbin/httpd
apache 27372   0.0  0.9   240994    9820 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27373   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27374   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27375   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27376   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27377   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27378   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
root   27817   0.0  0.0   103244     824 tty1 S+    13:40   0:00   grep httpd

My server is running fCGI, by the way.

For completeness sake, I made a small upload script with a web form:

$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
   $max_filesize = 1024000; // Maximum filesize in BYTES (currently 1MB).
   $upload_path = '/uploads/'; // The place the files will be uploaded to (currently a 'uploads' directory).


   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');

   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');

   // Check if we can upload to the specified path, if not DIE and inform the user.
   //if(!is_writable($upload_path))
   //   die('You cannot upload to '. $upload_path .'the specified directory, please CHMOD it to 777.');

   // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)){
         echo 'Your file upload was successful, view the file here'; // It worked.
     } else {
         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
 }

What am I missing here?

1
echo $upload_path . $filename and check file path is ok.Rukmi Patel
It echoes /uploads/logoklein.jpg, which is the correct path and filename.Dirk de Man
Could it be that the directory upload/ is missing?Louis Huppenbauer
The directory upload/ is there, currently chmodded to 0777...Dirk de Man
Remove the first / in the upload_path variable (or replace it with an absolute path like /var/www/vhosts/example.com/httpdocs/upload/). PHP probably tries to move the file into folder /upload, a unix file structure root folder, and not into the apache folder.Louis Huppenbauer

1 Answers

1
votes

Louis Huppenbauer in the comments had the right answer for my similar problem.

Using a relative path wasn't working, it wanted a full path. Try changing /uploads/ to the full directory of your host, eg: D:\InetPub\Vhosts\website.com\website2.com\uploads\