Jump to content

Image upload - to allow different extentions or file types?


Recommended Posts

My image upload, resize & rename script works fine for .jpg files. It sends the uploaded image names to the DB and save the files in a set folder. What should I do to my code to accept other image file type for images png, gif, tiff?

 

My field script as follow: (This is to allow only 4 images to be uploaded)

  for($i=1;$i<=$pictot;$i++){
echo '<tr>
<td>-</td><td>-</td><td>-</td><td>-</td><td>Image '.$i.'</td><td><input type="file" name="img'.$i.'"></td>
</tr>';
}

 

And then the upload script, after the form is submitted:

$imgNumb=1; //This the "pointer" to images
$DestinationDir="../ad_images/";  //Place the destination dir here
$ThumbDir="../ad_images/thumb/";  //Place the thumb dir here

do{
if($_FILES["img$imgNumb"][tmp_name]!=''){
$success++;
$Unique=microtime(); // We want unique names, right?
$destination=$DestinationDir.md5($Unique).".jpg";
$thumb=$ThumbDir.md5($Unique).".jpg";
$IMG=getimagesize($_FILES["img$imgNumb"][tmp_name]);
$finalimage = resize($_FILES["img$imgNumb"][tmp_name], 600, $destination);
//use the filename variable below to record the image name entered so that you can then use this variable to update your database if
$filename = md5($Unique).".jpg";
$field = 'img'.$imgNumb;
//mysql update your database fields
$query = mysql_query("INSERT INTO ad_image (ad, user, image) 
  VALUES ('$adnr', '$user', '$filename')") or die(mysql_error());
}

$imgNumb++;
} while($_FILES["img$imgNumb"][name]);

if($success>=1){
echo $im1;
echo $im2;
echo $im3;
echo $im4;
//$message = "Images uploaded!";
//echo $message;
}

First you need to check the file extension is allowed. There's plenty of methods both on here and Google if you do a quick bit of searching, they shouldn't be hard to implement. However you've hard-coded ".jpg" onto the end of your files, and I'll bet your resize() function doesn't cater for anything for other. The problem here being PHP use specific functions for different image formats whilst manipulating them, for example take a look at the imagejpeg function, and note the other functions in the 'see also' section.

 

Post the content of the resize() function and should be able to help...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.