Page 1 of 1

file upload problem

Posted: Thu Aug 23, 2007 3:04 am
by m2babaey
Hi
my image upload was working ok on local but sends error on the host.
this is the error:
Warning: move_uploaded_file(uploads/similar_project.gif) [function.move-uploaded-file]: failed to open stream: No such file or directory in /mnt/gs02/herd02/20421/domains/netfreelancer.com/html/index.php on line 756

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpt1eYVN' to 'uploads/similar_project.gif' in /mnt/gs02/herd02/20421/domains/netfreelancer.com/html/index.php on line 756
and the code:

Code: Select all

if($_FILES['file']['name']!=""){
	echo "ssss0";
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpg")
&& ($_FILES["file"]["size"] < 205000))
  {
  echo "ssss1";
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
      $user=$_COOKIE['jfuser'];
      $getuserid="SELECT id FROM jf_users WHERE username='$username' ";
    $res=mysql_query($getuserid) or die(mysql_error());
    $row1=mysql_fetch_array($res);
    $user_id=$row1['id'];
    $check="SELECT * FROM images WHERE  user_id='$user_id' && active=1 ";
    $res2=mysql_query($check)or die(mysql_error());
    $num=mysql_num_rows($res2);
    echo $num;
//    if ($num==10){ echo "You have uploaded the muximum of 10 images";}
//    else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
       $name= $_FILES["file"]["name"];
      $ar=explode(".",$name);
      $ext=$ar[1];
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      $type=$_FILES["file"]["type"];
      if ($type=="image/gif"){
      $newname=md5(time());
      rename('uploads/'. $_FILES["file"]["name"],'images/'.$newname.'.gif');
      $iname=$newname.'.gif';
      $insertdb="INSERT INTO images (user_id,name) VALUES ('$user_id','$iname') ";
      $res_do=mysql_query($insertdb)or die(mysql_error());
      }
      if($type=="image/jpg"){
       $newname=md5(time());
      rename('uploads/'. $_FILES["file"]["name"],'images/'.$newname.'.jpg');
        $iname=$newname.'.jpg';
      $insertdb="INSERT INTO images (user_id,name) VALUES ('$user_id','$iname') ";
      $res_do=mysql_query($insertdb)or die(mysql_error());
     }
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
  ///  }
    }
  }
else
  {
  echo "Invalid file. Only gif or jpg files below 200Kb are accepted";
  }
	}

Posted: Thu Aug 23, 2007 3:56 am
by Kieran Huggins
touch() the new filename before trying to move something there - this is a recurring problem for many with PHP.

Posted: Thu Aug 23, 2007 3:59 am
by aceconcepts
It looks like the "move to" directory is relatively incorrect to the source directory.

Check that "uploads/..." is relatively accurate.

Posted: Thu Aug 23, 2007 5:17 am
by Steve Mellor
Kieran Huggins wrote:touch() the new filename before trying to move something there - this is a recurring problem for many with PHP.
Remember though that doing so will change the owner of the file so you might want to consider chmod() to set the permissions you want as well.

Posted: Thu Aug 23, 2007 8:26 am
by m2babaey
the folder on the local was uploads and its name was images on the host.
sometimes i wonder how i lose my time :cry: :roll:

Posted: Thu Aug 23, 2007 8:57 am
by superdezign
A good suggestion for uploading is to always use absolute paths. A good way is to use dirname(__FILE__) and then concatenate '/..' up to the root, then give your class (or function or whatever that is) the path relative to the public root. i.e.

Code: Select all

// This is at: /root/path/to/public_html/classes/upload.php
class Upload
{
    protected $__root;

    public function __construct()
    {
        $this->__root = dirname(__FILE__) . '/../';
    }

    public function upload($fieldName, $destination)
    {
        move_uploaded_file($_FILES[$fieldName]['tmp_name'], $this->__root . $destination);
    }
}