Jump to content

[SOLVED] Image upload script doesn't upload the image


John_S

Recommended Posts

Hello everybody,I have recently created an image upload script which not only uploads and resizes the image but also creates a small thumbnail. After that the script saves both thumb and the resized image in two different folders and adds the information into the database.

However when I try to test it, I receive an error. Can somebody please help me and tell me what's wrong? I've checked everything but it seems I am either blind or simply can't see the error.

Here is my code:

<?phpif ((isset($_POST["image_upload"])) && ($_POST["submitted_form"] == "image_upload_form")) {
// file needs to be jpg,gif,png and 4 MB max if (($_FILES["image"]["type"] == "image/jpeg" || $_FILES["image"]["type"] == "image/pjpeg" || $_FILES["image"]["type"] == "image/gif" || $_FILES["image"]["type"] == "image/png") || $_FILES["image"]["type"] == "image/bmp" && ($_FILES["image"]["size"] < 4000000))   {      
// some settings      $max_upload_width = 550;      $max_upload_height = 550;            
// if uploaded image was JPG/JPEG      if($_FILES["image_upload"]["type"] == "image/jpeg" || $_FILES["image"]["type"] == "image/pjpeg") {           $image_source = imagecreatefromjpeg($_FILES["image_upload"]["tmp_name"]);         $extension = '.jpeg';      }         
// if uploaded image was GIF      if($_FILES["image_upload"]["type"] == "image/gif") {            $image_source = imagecreatefromgif($_FILES["image_upload"]["tmp_name"]);         $extension = '.gif';      }            
// if uploaded image was PNG      if($_FILES["image_upload"]["type"] == "image/png") {         $image_source = imagecreatefrompng($_FILES["image_upload"]["tmp_name"]);         $extension = '.png';      }      
//If uploaded file was BMP.      if($_FILES["image_upload"]["type"] == "image/png") {         $image_source = imagecreatefromwbmp($_FILES["image_upload"]["tmp_name"]);         $extension = '.bmp';      }
   $thumnail_name = $_FILES["image_upload"]["name"];                  
/////////////////////////////////      // Transform the main image.      /////////////////////////////////      
$_FILES["image_upload"]["name"] = uniqid().$extension;            $remote_file = "images/uploads/".$_FILES["image_upload"]["name"];      imagepng($image_source, $remote_file, 100);      chmod($remote_file, 0755);      // Get width and height of original image      list($image_width, $image_height) = getimagesize($remote_file);           
if($image_width>$max_upload_width || $image_height >$max_upload_height) {         $proportions = $image_width/$image_height;                  
if($image_width>$image_height){$new_width = $max_upload_width;            $new_height = round($max_upload_width/$proportions);         }  
else{            $new_height = $max_upload_height;            $new_width = round($max_upload_height*$proportions);         }                           
$new_image = imagecreatetruecolor($new_width , $new_height);                  $image_source = imagecreatefrompng($remote_file);         }            imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);      imagejpeg($new_image,$remote_file,100);               
imagedestroy($new_image);      imagedestroy($image_source);                  
/////////////////////////////////      // Transform the thumbnail image.      /////////////////////////////////      
$max_thumb_width = 90;      $max_thumb_height = 90;            //The procedure.      $thumnail_name = uniqid()."_thumb".$extension;            $remote_thumbnail = "images/uploads/thumbs/".$thumnail_name;      imagepng($thumbnail_source, $remote_thumbnail, 100);      chmod($remote_thumbnail,0755);
      // Get width and height of original image      list($image_width, $image_height) = getimagesize($remote_thumbnail);            if($image_width>$max_thumb_width || $image_height >$max_thumb_height) {         $proportions = $image_width/$image_height;                  
if($image_width>$image_height){            $new_width = $max_thumb_width;            $new_height = round($max_thumb_width/$proportions);         }               
else{        $new_height = $max_thumb_height;            $new_width = round($max_thumb_height*$proportions);         }                  
$new_image = imagecreatetruecolor($new_width , $new_height);                  $image_source = imagecreatefrompng($remote_thumbnail);         }            imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);      imagejpeg($new_image,$remote_thumbnail,100);               imagedestroy($new_image);      imagedestroy($image_source);            
$db->query("INSERT INTO uploads_images (id,name,url,thumb_url,added_by,date) VALUES (NULL,'".$_FILES["image_upload"]["name"]."','".$remote_file."','".$remote_thumbnail."','".$mybb->user['username']."', '".$date."')");            echo '<blockquote class="green">Image uploaded successfully!</blockquote>';   }      
else {      echo '<blockquote class="red">An error was encountered during the image transfer. Please make sure the file is jpg, gif, png or bmp and that is smaller than 4MB! !</blockquote>';      }}?>
<h1 style="margin-bottom: 0px">Submit an image</h1>
<div class="article"><div class="article_content"><p>Browse for the file you want to upload and click "Upload".</p>
<form action="uploads.php" method="post" enctype="multipart/form-data" name="image_upload" id="image_upload" style="margin-bottom:0px;">      
<label>Your file, maximum 4MB. it can be jpg, gif,  png:</label><br />        <input name="image_upload" type="file" id="image" size="40" />        <input type="submit" name="submit" value="Upload image" />           <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" /></form>
<?php if(isset($remote_thumbnail)) {   echo '<p><center><a href="'.$remote_thumbnail.'" rel="lightbox"><img src="'.$remote_thumbnail.'" alt="Picture of the week" /></a></center><br /></p>';}?>
</div></div>

 

Firstly, you need to tell us what this error you say your getting is.

 

Secondly, if you expect anyone to even look at your code, format it so it is readable.

 

That is what I am trying to do, and about the error, I don't get any at all, that's the problem. No error at all but the files are not created in the folders.

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.