Page 1 of 1

problems with image upload code

Posted: Tue Jun 09, 2009 9:06 am
by dom_no_10
Hello everyone!
I am not new to PHP but I have this really wierd problem...

I have taken this php image upload code from a website on the internet but for some reason, it only accepts files that are small both in file size and physical size. I am pretty sure that the problem does not lie with the file size as I am attempting to upload 900kb files and have set the file limit to 4mb. This code works for smaller files that are about 300px and about 300kb in size.

Code: Select all

                            error_reporting(0);
                            $change="";
                            $abc="";
                             define ("MAX_SIZE","4096");
                             function getExtension($str) {
                                     $i = strrpos($str,".");
                                     if (!$i) { return ""; }
                                     $l = strlen($str) - $i;
                                     $ext = substr($str,$i+1,$l);
                                     return $ext;
                             }
 
                             $errors=0;
 
                             if($_SERVER["REQUEST_METHOD"] == "POST")
                             {
                                $image =$_FILES["new_image"]["name"];
                                $uploadedfile = $_FILES['new_image']['tmp_name'];
                                if ($image) 
                                {
                                    $filename = stripslashes($_FILES['new_image']['name']);
                                    $extension = getExtension($filename);
                                    $extension = strtolower($extension);
                             if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
                                    {
                                        $change='<div class="msgdiv">Unknown Image extension </div> ';
                                        $errors=1;
                                    }
                                    else
                                    {
                             $size=filesize($_FILES['new_image']['tmp_name']);
 
                            if ($size > MAX_SIZE*1024)
                            {
                                $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
                                $errors=1;
                            }
                            if($extension=="jpg" || $extension=="jpeg" )
                            {
                            $uploadedfile = $_FILES['new_image']['tmp_name'];
                            $src = imagecreatefromjpeg($uploadedfile);
                            }
                            else if($extension=="png")
                            {
                            $uploadedfile = $_FILES['new_image']['tmp_name'];
                            $src = imagecreatefrompng($uploadedfile);
                            }
                            else 
                            {
                            $src = imagecreatefromgif($uploadedfile);
                            }
 
                            list($width,$height)=getimagesize($uploadedfile);
                            
                            $newwidth=300;
                            $newheight=($height/$width)*$newwidth;
                            $tmp=imagecreatetruecolor($newwidth,$newheight);
                            
                            $newwidth1=110;
                            $newheight1=($height/$width)*$newwidth1;
                            $tmp1=imagecreatetruecolor($newwidth1,$newheight1);
 
                            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
 
                            imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
 
                            $filename = "gallery_images/". $_FILES['new_image']['name'];
                            $filename1 = "gallery_images/small". $_FILES['new_image']['name'];
                            imagejpeg($tmp,$filename,100);
 
                            imagejpeg($tmp1,$filename1,100);
 
                            imagedestroy($src);
                            imagedestroy($tmp);
                            imagedestroy($tmp1);
                            }}
 
                            }
 
                            //If no errors registred, print the success message
                             if(isset($_POST['Submit']) && !$errors) 
                             {
                                $change=' <div class="msgdiv">Image Uploaded Successfully!</div>';
                             }
 




I would really appreciate anyones thoughts and views on this.


Thanks

Dominic

Re: problems with image upload code

Posted: Tue Jun 09, 2009 2:45 pm
by Raph
Looks to me that you resize the images to 300px on row 55, but I might be mistaken.