file 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
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

file upload problem

Post 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";
  }
	}
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

touch() the new filename before trying to move something there - this is a recurring problem for many with PHP.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

It looks like the "move to" directory is relatively incorrect to the source directory.

Check that "uploads/..." is relatively accurate.
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post 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.
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post 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:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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);
    }
}
Post Reply