Uploading script doesn't work

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Uploading script doesn't work

Postby jcafaro10 » 10. June 2008 18:54

I'm trying to do a simple script that uploads a file. I copied the code from a tutorial. I have xampp installed and running and I have a folder called uploads on the same level as htdocs but when I run the script and try to upload I get this error:

Warning: move_uploaded_file(../uploads/return fire v5 copy.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\Program Files\xampp\htdocs\PHPTest1\upload_image.php on line 28

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Program Files\xampp\tmp\php27.tmp' to '../uploads/return fire v5 copy.jpg' in C:\Program Files\xampp\htdocs\PHPTest1\upload_image.php on line 28


Here's my code (I have a comment in line 28):
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <title>Upload an Image</title>
   <style type="text/css" title="text/css" media="all">
   .error {
      font-weight: bold;
      color: #C00
   }
   </style>
</head>
<body>
<?php # Script 10.3 - upload_image.php

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

   // Check for an uploaded file:
   if (isset($_FILES['upload'])) {
      
      // Validate the type. Should be JPEG or PNG.
      $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
      if (in_array($_FILES['upload']['type'], $allowed)) {
      
         // Move the file over.
         if (move_uploaded_file ($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']}")) { //THIS IS LINE 28
            echo '<p><em>The file has been uploaded!</em></p>';
         } // End of move... IF.
         
      } else { // Invalid type.
         echo '<p class="error">Please upload a JPEG or PNG image.</p>';
      }

   } // End of isset($_FILES['upload']) IF.
   
   // Check for an error:
   
   if ($_FILES['upload']['error'] > 0) {
      echo '<p class="error">The file could not be uploaded because: <strong>';
   
      // Print a message based upon the error.
      switch ($_FILES['upload']['error']) {
         case 1:
            print 'The file exceeds the upload_max_filesize setting in php.ini.';
            break;
         case 2:
            print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
            break;
         case 3:
            print 'The file was only partially uploaded.';
            break;
         case 4:
            print 'No file was uploaded.';
            break;
         case 6:
            print 'No temporary folder was available.';
            break;
         case 7:
            print 'Unable to write to the disk.';
            break;
         case 8:
            print 'File upload stopped.';
            break;
         default:
            print 'A system error occurred.';
            break;
      } // End of switch.
      
      print '</strong></p>';
      
   } // End of error IF.
   
   // Delete the file if it still exists:
   if (file_exists ($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) ) {
      unlink ($_FILES['upload']['tmp_name']);
   }
         
} // End of the submitted conditional.
?>
   
<form enctype="multipart/form-data" action="upload_image.php" method="post">

   <input type="hidden" name="MAX_FILE_SIZE" value="524288">
   
   <fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend>
   
   <p><b>File:</b> <input type="file" name="upload" /></p>
   
   </fieldset>
   <div align="center"><input type="submit" name="submit" value="Submit" /></div>
   <input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
jcafaro10
 
Posts: 15
Joined: 16. May 2008 05:43

Postby Wiedmann » 10. June 2008 20:27

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Program Files\xampp\tmp\php27.tmp' to '../uploads/return fire v5 copy.jpg' in C:\Program Files\xampp\htdocs\PHPTest1\upload_image.php on line 28

You should use an absolute path for the target.

There can also be a problem with the upload, but you are testing for upload errors after move_uploaded_file()..
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby jcafaro10 » 12. June 2008 16:33

why would I want to use an absolute path? Especially if uploads aren't accessible on the web server? Why doesn't it work like this?
jcafaro10
 
Posts: 15
Joined: 16. May 2008 05:43

Postby jcafaro10 » 12. June 2008 16:40

Figured it out. Had to go up one more directory
jcafaro10
 
Posts: 15
Joined: 16. May 2008 05:43

Postby Wiedmann » 12. June 2008 16:54

why would I want to use an absolute path?

move_uploaded_file() is (most times) not working relative to the script directory.

But you must not hardcode this path, you can build this absolute path with realpath() or dirname() relative to the current script dir, and then use this as param for move_uploaded_file().
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 50 guests