Jump to content

Recommended Posts

Hello i made this script to upload images (jpg and gif) to my server, the script works but it wont upload gif images for some reason...!

if((!empty($_FILES["poster"])) && ($_FILES['poster']['error'] == 0)) {
  //Check if the file is JPEG or GIF image and it's size is less than 350Kb
  $filename = basename($_FILES['poster']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  //Determine the path to which we want to save this file
  $newname = dirname(__FILE__).'/files/'.$filename;
  if (($ext != "jpg") || ($ext != "gif") &&                      //  <=== I THINK THE ERROR IS HERE
    ($_FILES["poster"]["size"] > 350000)) { 
  $error .= "<strong>Error</strong>: Image must be .jpg or .gif and less than 350kb.<br>";  
  }
      //Check if the file with the same name is already exists on the server
      if (file_exists($newname)) {
      $error .= "<strong>Error</strong>: Image exist<br>";
      } 
  } 

if (!$error) {
if ((move_uploaded_file($_FILES['poster']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        }
}

 

Thanks in advance...!

Link to comment
https://forums.phpfreaks.com/topic/74390-uploading-image-script/
Share on other sites

Yea that should be an or statement || instead of &&

 

So your code would be saying if the extension is not .jpg or not .gif or its bigger than the maximum size you want for uploads...then display the error.

I tried but still aint working at all... they way it is its working but only for jpg....

gonna save you some headaches, as you probably haven't tested it in IE, but Ie uses the pjpeg, pjpg type.  I'll give you this since I posted it a few days ago, just modify the path and the desired size and it will work for you.  Note the field name I used was image.  Also this uploads multi types as their type to prevent any loss (I couldn't use imagestick to modify them), so you will need SQL backend to track this.

<?php
//Allowed types of images
$img_types = array("image/jpg", "image/pjpg", "image/jpeg", "image/pjpeg", "image/png", "image/gif");
//Test if its valid first
if (in_array($_FILES['image']['type'], $img_types)){ 
switch ($_FILES['image']['type']){
	case "image/gif":
		$new_path = ROOT."graphics/".$_SESSION['FacID'].".gif";
		$type = "gif";
	break;
	case "image/png":
		$new_path = ROOT."graphics/".$_SESSION['FacID'].".png";
		$type = "png";
	break;
	default:
		$new_path = ROOT."graphics/".$_SESSION['FacID'].".jpg";
		$type = "jpg";
	}
if(copy ($_FILES['image']['tmp_name'], $new_path)){
    list($width, $height) = getimagesize($new_path);
//Target max height and or width
	$target = 150;
	if ($width > $height) {
		$percentage = ($target / $width);
	}
	else {
		$percentage = ($target / $height);
	}
	//gets the new value and applies the percentage, then rounds the value
	$width1 = round($width * $percentage);
	$height1 = round($height * $percentage); 
	$imgdata = getimagesize($new_path);

	if($type == "jpg"){
	$source = imagecreatefromjpeg($new_path);
	$thumb = ImageCreateTrueColor($width1, $height1);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $width1, $height1, $width, $height) or die("error copyresize");
	imagejpeg($thumb,$new_path,100);
//This updates my SQL to reflect the right type so it can be displayed right.
	$q = "Update `".F_TABLE."` Set Imgtype = 'jpg' Where FacID = '".$_SESSION['FacID']."'";
	$r = mysql_query($q)or die(mysql_error());
	}
	elseif($type == "gif"){
		$source = imagecreatefromgif($new_path);
		$thumb = ImageCreateTrueColor($width1, $height1);
		imagecopyresized($thumb, $source, 0, 0, 0, 0, $width1, $height1, $width, $height);
		imagegif($thumb,$new_path);
//This updates my SQL to reflect the right type so it can be displayed right.
		$q = "Update `".F_TABLE."` Set Imgtype = 'gif' Where FacID = '".$_SESSION['FacID']."'";
		$r = mysql_query($q)or die(mysql_error());
	}
	elseif($type == "png"){
		$source = imagecreatefrompng($new_path);
		$thumb = ImageCreateTrueColor($width1, $height1);
		imagecopyresized($thumb, $source, 0, 0, 0, 0, $width1, $height1, $width, $height);
		imagepng($thumb,$new_path,100);
//This updates my SQL to reflect the right type so it can be displayed right.
		$q = "Update `".F_TABLE."` Set Imgtype = 'png' Where FacID = '".$_SESSION['FacID']."'";
		$r = mysql_query($q)or die(mysql_error());
	}
	$q = "Update `".F_TABLE."` Set Image='1' Where FacID ='".$_SESSION['FacID']."'";
	$r = mysql_query($q) or die(mysql_error());
}


else{
	$errors[] = "The Image could not be uploaded please try again<br />".$_FILES['image']['tmp_name']."<br />".$new_path;
}
}
else{
if(!empty($_FILES['image']['tmp_name'])){
	$errors[] = "Invalid File Type must be a jpeg";
}
}
?>

Thanks for the code, i might use it if no one will find an error in my script, i hop someone does coz i think is a little stupid thing i have done wrong and its not working... i think the error is somewere:

if (($ext != "jpg") || ($ext != "gif") && // I Think is here... just cant fix it...!!!
    ($_FILES["poster"]["size"] > 350000)) {

Am afraid its still not working  :( :( :(! Now its just uploads anything.... And i wont it to give the error if the file is not jpg or gif...

 

if (($ext != "jpg") || ($ext != "gif") || // I Think is here... just cant fix it...!!!
    ($_FILES["poster"]["size"] > 350000)) {

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.