Problem uploading image

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
petrosa
Forum Newbie
Posts: 12
Joined: Fri Feb 27, 2009 9:13 pm

Problem uploading image

Post by petrosa »

Hello,

I have just transfered my webpage to another server, and now the script that uploads an image file doesnt work. All i get is the "Sorry, there was a problem uploading your file." from when the image fails to trasfer.

What might be the source of the problem?

Here is the script

Code: Select all

 
$rand1 = rand();
$target = dirname( __FILE__ )."photos/";
$target1 = $target . $rand1 . basename( $_FILES['photo1']['name']);
 
if (($_FILES['photo1']['name']) == ""){
     $pic1="default.gif";
}
else{
     $pic1 = $rand1 . basename( $_FILES['photo1']['name']);
 
     if (!getimagesize($_FILES['photo1']['tmp_name'])){
          echo "Invalid Image File...";
          exit();
     }
}
 
$name = $_REQUEST['name'];
$telephone = $_REQUEST['telephone'];
$address = $_REQUEST['address'];
$lat = $_REQUEST['lat'];
$lng = $_REQUEST['lng'];
$date=time();
 
if (($_FILES['photo1']['name']) != ""){
 
   //Writes the photo to the server
   if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1))
   {
      //Tells you if its all ok
      echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and  your information has been added to the directory";
 
      //create thumbanail
      $n_width=100; // thumbanil width
      $n_height=100; // thumbnail height
      $thumbs = "thumbnails/";
      $thumbs = $thumbs . $rand1 . basename( $_FILES['photo1']['name']);
 
      //if($_FILES['photo1'][type]=="image/jpeg"){ 
          $im=ImageCreateFromJPEG($target1);
          $width=ImageSx($im); // Original picture width is stored
          $height=ImageSy($im); // Original picture height is stored
          $newimage=imagecreatetruecolor($n_width,$n_height);
          imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
          ImageJpeg($newimage,$thumbs);
          //chmod("$thumbs",0777);
      //}
      // end thumbnail creation
   }
   else {
      //Gives and error if its not
      echo "Sorry, there was a problem uploading your file.";
   } 
}
 
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Problem uploading image

Post by susrisha »

try to echo $_FILES['error'] in the error reporting statement (sorry the file could not be uploaded) and try to look for the error codec that corresponds to the error.
Mostly the permissions problem
petrosa
Forum Newbie
Posts: 12
Joined: Fri Feb 27, 2009 9:13 pm

Re: Problem uploading image

Post by petrosa »

Thanks for tou answer, i did what you said and i got value 0 from it, which from php manual means "There is no error, the file uploaded with success". The error is printed when the file is being moved to the directory i want, which fails.

I think it might be a permissions error, but when i try to chmod 777 the folder where it gets uploaded from filezilla, i get a command not understood.

Command: SITE CHMOD 777 photos
Response: 500 'SITE CHMOD 777 photos': command not understood

Should i contact the administrator for this, or is there another way around ?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Problem uploading image

Post by mikemike »

Is the host Windows or UNIX based? Windows doesn't do chmod'ing
petrosa
Forum Newbie
Posts: 12
Joined: Fri Feb 27, 2009 9:13 pm

Re: Problem uploading image

Post by petrosa »

I am not sure of the server (any easy way to find out?), but if it was windows would i have this problem?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Problem uploading image

Post by McInfo »

petrosa wrote:

Code: Select all

$target = dirname( __FILE__ )."photos/";
If your file is located at "/gallery/index.php", $target will be "/galleryphotos/". If you want it to be "/gallery/photos/", you need to change line 3 to

Code: Select all

$target = dirname( __FILE__ )."/photos/";
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 10:06 pm, edited 1 time in total.
petrosa
Forum Newbie
Posts: 12
Joined: Fri Feb 27, 2009 9:13 pm

Re: Problem uploading image

Post by petrosa »

McInfo wrote:
petrosa wrote:

Code: Select all

$target = dirname( __FILE__ )."photos/";
If your file is located at "/gallery/index.php", $target will be "/galleryphotos/". If you want it to be "/gallery/photos/", you need to change line 3 to

Code: Select all

$target = dirname( __FILE__ )."/photos/";
Made the change, but still no luck. I added dirname( __FILE__ ) when i was trying to fix the problem. It still cant move the uploaded file to my directory.

Could it be something about the server settings?
Post Reply