gtal3x Posted October 23, 2007 Share Posted October 23, 2007 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...! Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/ Share on other sites More sharing options...
sdi126 Posted October 23, 2007 Share Posted October 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/#findComment-375865 Share on other sites More sharing options...
gtal3x Posted October 23, 2007 Author Share Posted October 23, 2007 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.... Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/#findComment-375870 Share on other sites More sharing options...
gtal3x Posted October 23, 2007 Author Share Posted October 23, 2007 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/#findComment-375887 Share on other sites More sharing options...
gtal3x Posted October 23, 2007 Author Share Posted October 23, 2007 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/#findComment-376645 Share on other sites More sharing options...
cooldude832 Posted October 23, 2007 Share Posted October 23, 2007 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"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/#findComment-376649 Share on other sites More sharing options...
gtal3x Posted October 23, 2007 Author Share Posted October 23, 2007 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)) { Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/#findComment-376653 Share on other sites More sharing options...
BlueSkyIS Posted October 23, 2007 Share Posted October 23, 2007 if (($ext != "jpg") || ($ext != "gif") || // I Think is here... just cant fix it...!!! ($_FILES["poster"]["size"] > 350000)) { Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/#findComment-376656 Share on other sites More sharing options...
gtal3x Posted October 24, 2007 Author Share Posted October 24, 2007 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)) { Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/#findComment-376659 Share on other sites More sharing options...
teng84 Posted October 24, 2007 Share Posted October 24, 2007 no need to use string functions just do it this way if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")) && ($_FILES["file"]["size"] < 20000)) Quote Link to comment https://forums.phpfreaks.com/topic/74390-uploading-image-script/#findComment-376687 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.