Jump to content


Photo

File upload problems specifically move_uploaded_file function


  • Please log in to reply
1 reply to this topic

#1 venkir

venkir
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 July 2006 - 08:56 AM

Hi all Gurus:
I have not been able to make file upload work for about 2 days now and am at the end of my wits

-----------Code Snippet ----------------------
<?php
  require ('page.inc');
 
  // Check for error
  $userfile_error = $_FILES['userfile']['error'];
  if ( $userfile_error > 0)
  {
  $pagecontent =  $pagecontent . 'Problem :';
  switch ($userfile_error)
    {
      case 1:  $pagecontent =  $pagecontent . 'File exceeded upload_max_filesize';  break;
      case 2:  $pagecontent =  $pagecontent . 'File exceeded max_file_size';  break;
      case 3:  $pagecontent =  $pagecontent . 'File only partially uploaded';  break;
      case 4:  $pagecontent =  $pagecontent . 'No file uploaded';  break;
    }
    exit;
  }
 
  // Extract Form fields
  $userfile = $_FILES['userfile']['name'];
  // Check the input values 
  if ( empty($userfile))
  {
  echo 'One of the required fields was empty, please check and try again';
  }
  else
  {
  // Put the file where we want it
  $upfile = '/uploads/' . $userfile; 
 
  $tmpfile = 'c:\\wamp\\tmp\\' . basename($_FILES['userfile']['tmp_name']);
  echo $tmpfile . " <--- used basename and appended to the path <br>";
  echo $_FILES['userfile']['tmp_name'] . "<--- Original tmp file<br>";
  echo $_FILES['userfile']['name'] . " <--- FILES name var <br>";
  echo $_FILES['userfile']['size'] . " <--- FILES size var<br>";
  $pagecontent = $pagecontent . $_FILES['userfile']['type'] . " <--- FILES type var<br>";
 
  if (is_uploaded_file($_FILES['userfile']['tmp_name']))
  {
  if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
  {
  echo 'Error: Could not move file to destination directory.';
  }
  else
  {
  echo "<h2>Processing File:" . $upfile. "</h2>\n<br>";
  }
  }
  else
  {
  echo 'Error: Possible file upload attack :'. $_FILES;
  }
?>
-----------End Code Snippet ----------------------
and the results in the browser are

c:\wamp\tmp\php4B.tmp <--- used basename and appended to the path
c:/wamp/uploads\php4B.tmp<--- Original tmp file
angus_input.csv <--- FILES name var
1906 <--- FILES size var
text/plain <--- FILES type var
Error: Could not move file to destination directory.


with addtional warnings

Warning: move_uploaded_file(/uploads/angus_input.csv) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\wamp\www\GeoCoding\geocodecsv.php on line 49

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'c:/wamp/uploads\php4B.tmp' to '/uploads/angus_input.csv' in C:\wamp\www\GeoCoding\geocodecsv.php on line 49


Clearly the problem is the tmp upload file full path name which is 'c:/wamp/uploads\php4B.tmp'  (NOTICE the different slash direction)

How do I fix this? I tried stripslashes, tried re-constructing the tmp file and then providing it to function move_uploaded_file, but it still does not move it

Can anyone help? Thanks a lot in advance
-Venki

#2 venkir

venkir
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 July 2006 - 09:53 AM

I fixed my own problem. I hardcoded the entire path instead of using the relative path for the destination filename and for some reason  it worked. All documentaion says that it is relative to where the php script executes. Anyway that was my fix if it helps anybody.

-Venki




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users