Upload problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Upload problem

Post 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
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Path correct?
Permissions correct?
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

Path and file permissions appear to be correct, Im just getting that strange error?
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

Damn it! It still doesn't work. It gives the identically same errors though.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

what are the permissions on the destination directory?
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

The permissions for the folder are set to chmod 777
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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)
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post 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>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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?
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

Trye using the full path for the $upfile variable

i.e.

Code: Select all

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