Jump to content

[SOLVED] Image upload not working!


ukweb

Recommended Posts

Hi. I've got the following code to upload an image file, and when you upload to a single location (without the 'thumb' upload it works fine, but as I want the image to be uploaded twice, it won't work. it fails at the point of checking whether the thumbs directory is writable, and it definitely is but it fails!

 

Can anyone help at all?? THe code is below! THanks!

 

  // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); 
      $max_filesize = 524288; 
      $upload_path = 'user_files/uploaded/images/uploaded/'; 
      $upload_thumb_path = $upload_path.'thumbs/'; 

   $filename = $_FILES['userfile']['name']; 
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');

   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');

   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');

   if(!is_writable($uploa_thumbd_path))
      die('You cannot upload to the specified directory thumb, please CHMOD it to 777.');

   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed .
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_thumb_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed .

Link to comment
https://forums.phpfreaks.com/topic/72632-solved-image-upload-not-working/
Share on other sites

Right, I've corrected the spelling and now it isn't bringing up an error for the path not being writable, however, the main image is uploading but the 'thumb' one isn't its just returning the error. I can't see why.

  // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); 
      $max_filesize = 524288; 
      $upload_path = 'user_files/uploaded/images/uploaded/'; 
      $upload_thumb_path = $upload_path.'thumbs/'; 

   $filename = $_FILES['userfile']['name']; 
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');

   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');

   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');

   if(!is_writable($upload_thumb_path))
      die('You cannot upload to the specified directory thumb, please CHMOD it to 777.');

   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed .
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_thumb_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed .

 

 

I've got it working now by using the following code! Thanks for everyone's help!

  // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); 
      $max_filesize = 524288; 
      $upload_path = 'user_files/uploaded/images/uploaded/'; 
      $upload_thumb_path = $upload_path.'thumbs/'; 

   $filename = $_FILES['userfile']['name']; 
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');

   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');

   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');

   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) {
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
	 if(copy($upload_path.$filename, $upload_thumb_path.$filename))
	 echo 'Thumbnail created successfully'.$filename;
	else
		echo 'Thumbnail hit a wall!';
	}
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed .

 

 

Hi, I had already marked it as solved, guess you viewed it jus b4 I hit the solved button, as the email telling me the topic had been replied to (basically when you posted on it) had "SOLVED" in the subject line!

 

Thanks for your help, I can go to bed now and not worry about any more PHP til the morning (I'm in the UK, its 00:34 here!!)

 

Nite all!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.