Page 1 of 1

Upload problem

Posted: Fri May 26, 2006 12:03 pm
by tommy1987
Hi there, I am trying to let people upload images onto my site and have the following HTML and PHP, there error i seem to be getting is this:

Code: Select all

Warning: move_uploaded_file(/uploads/amazon-logo-151x32.gif): failed to open stream: No such file or directory in /home/tom10001/public_html/496/uploadtest.php on line 44

Warning: move_uploaded_file(): Unable to move '/tmp/phpyBtSUZ' to '/uploads/amazon-logo-151x32.gif' in /home/tom10001/public_html/496/uploadtest.php on line 44
Problem: Could not move file to destination directory
Here is the HTML:

Code: Select all

<html>

  <head>
    <title>Upload images</title>
  </head>

  <body>
    <h1>Upload files</h1>
    <form enctype="multipart/form-data" action="uploadtest.php" method="post">
      <input type="hidden" name="MAX_FILE_SIZE" value="1000000">		
      Upload this file: <input name="userfile" type="file">
      <input type="submit" value="Send File">
    </form>
  </body>

</html>
The PHP is here:

Code: Select all

<html>
  <head>
    <title>Uploading...</title>
  </head>

  <body>
    <h1>Uploading file...</h1>
    <?php
      // $userfile is where the file went on webserver
      $userfile      = $HTTP_POST_FILES['userfile']['tmp_name'];
      // $userfile_name is original file name
      $userfile_name = $HTTP_POST_FILES['userfile']['name'];
      // $userfile_size is size in bytes
      $userfile_size = $HTTP_POST_FILES['userfile']['size'];
      // $userfile_type is mime type e.g. image/gif
      $userfile_type = $HTTP_POST_FILES['userfile']['type'];
      // $userfile_error is any error encountered
      $userfile_error= $HTTP_POST_FILES['userfile']['error'];

      if($userfile_error > 0)
      {
        echo 'Problem: ';
        switch ($userfile_error)
        {
          case 1: echo 'File exceeded upload_max_filesize'; break;
	  case 2: echo 'File exceeded max_file_size'; break;
          case 3: echo 'File only partially uploaded'; break;
          case 4: echo 'No file uploaded'; break;
        } 
        exit;
      }

      // Does the file have the correct mime type?
      if($userfile_type != 'image/gif')
      {
        echo 'Problem: file is not a GIF';
        exit;
      }

      $upfile = '/uploads/'.$userfile_name;

      if(is_uploaded_file($userfile))
      {
        if(!move_uploaded_file($userfile, $upfile))
	{
   	  echo 'Problem: Could not move file to destination directory';
  	  exit;
        }
      }
      else
      {
	echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
      }
      echo 'File uploaded successfully<br/><br/>';

      //reformat file contents
      $fp = fopen($upfile,'r');
      $contents = fread($fp, filesize ($upfile));
      fclose($fp);

      $contents = strip_tags($contents);
      $fp = fopen($upfile, 'w');
      fwrite($fp, $contents);
      fclose($fp);        
    ?>
</body>
</html>
Anyone got any ideas?

Thanks in advance... Tom

Posted: Fri May 26, 2006 12:07 pm
by PrObLeM
Path correct?
Permissions correct?

Posted: Fri May 26, 2006 12:13 pm
by tommy1987
Path and file permissions appear to be correct, Im just getting that strange error?

Posted: Fri May 26, 2006 12:18 pm
by PrObLeM
try making the path

Code: Select all

$upfile = 'uploads/'.$userfile_name;  //notice i took the first / out, that means it brings you to the root dir

Posted: Fri May 26, 2006 12:21 pm
by tommy1987
Damn it! It still doesn't work. It gives the identically same errors though.

Posted: Fri May 26, 2006 12:32 pm
by PrObLeM
what are the permissions on the destination directory?

Posted: Fri May 26, 2006 12:48 pm
by tommy1987
The permissions for the folder are set to chmod 777

Posted: Fri May 26, 2006 12:54 pm
by PrObLeM
is it possible that your disk quota is full? ( http://www.php.net/manual/en/function.m ... .php#58540 )
move_uploaded_file()'s return codes are not allways obious !

Unable to move '/var/tmp/phpuuAVJv' to '/home/me/website.com/upload/images/hello.png'

will apear if your disk is full, or the webserver (www user) exeeded it's disk qouta. (probably some others)

Posted: Fri May 26, 2006 1:00 pm
by tommy1987
No that isnt the problem, i think it is something to do with the file paths, i have just printed them out on the script and its getting:

local file: /tmp/php8GosGt

server file: public_html/uploads/dddddd.gif

now the problem with that is, where is this tmp folder???? Also im guessing that name is auto generated, because the original name of the file is dddddd.gif.

Don't know if this information helps but the folder I want the file in is: 'public_html/uploads/'

p.s the script from above has been modified to allow for this:

Code: Select all

<html>
  <head>
    <title>Uploading...</title>
  </head>

  <body>
    <h1>Uploading file...</h1>
    <?php
      // $userfile is where the file went on webserver
      $userfile      = $HTTP_POST_FILES['userfile']['tmp_name'];
      // $userfile_name is original file name
      $userfile_name = $HTTP_POST_FILES['userfile']['name'];
      // $userfile_size is size in bytes
      $userfile_size = $HTTP_POST_FILES['userfile']['size'];
      // $userfile_type is mime type e.g. image/gif
      $userfile_type = $HTTP_POST_FILES['userfile']['type'];
      // $userfile_error is any error encountered
      $userfile_error= $HTTP_POST_FILES['userfile']['error'];

      if($userfile_error > 0)
      {
        echo 'Problem: ';
        switch ($userfile_error)
        {
          case 1: echo 'File exceeded upload_max_filesize'; break;
	  case 2: echo 'File exceeded max_file_size'; break;
          case 3: echo 'File only partially uploaded'; break;
          case 4: echo 'No file uploaded'; break;
        } 
        exit;
      }

      // Does the file have the correct mime type?
      if($userfile_type != 'image/gif')
      {
        echo 'Problem: file is not a GIF';
        exit;
      }

      $upfile = 'public_html/uploads/'.$userfile_name;

      if(is_uploaded_file($userfile))
      {
	  	echo $userfile.'    '.$upfile;			//THIS IS WHERE I AM PRINTING THE FILE PATHS
        if(!move_uploaded_file($userfile, $upfile))
	{
   	  echo 'Problem: Could not move file to destination directory';
  	  exit;
        }
      }
      else
      {
	echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
	exit;
      }
      echo 'File uploaded successfully<br/><br/>';

      //reformat file contents
      $fp = fopen($upfile,'r');
      $contents = fread($fp, filesize ($upfile));
      fclose($fp);

      $contents = strip_tags($contents);
      $fp = fopen($upfile, 'w');
      fwrite($fp, $contents);
      fclose($fp);  
	  
    ?>
</body>
</html>

Posted: Fri May 26, 2006 4:49 pm
by timvw
tommy1987 wrote: server file: public_html/uploads/dddddd.gif
Since this is a relative path... Wich is the current working directory for your webserver?

Posted: Fri May 26, 2006 5:22 pm
by $phpNut
Trye using the full path for the $upfile variable

i.e.

Code: Select all

$upfile = '/home/username/public_html/uploads/' . $userfile_name