Saturday, October 11, 2014

code upload image with validation (perfect)

if(isset($_POST["btn_new_restaurant"])){
                $galleryname= $_POST['txt_gallery_text'];
$arrayfile=$_FILES["uploadedfile"]["name"] ;
                $tmp_file = $_FILES["uploadedfile"]['tmp_name'];
                $gl_status=$_POST['gl_status'];
$content=$_POST['content'];
                define ("MAX_SIZE","1024");
                function getExtension($str) {
                     $i = strrpos($str,".");
                     if (!$i) { return ""; }
                     $l = strlen($str) - $i;
                     $ext = substr($str,$i+1,$l);
                     return $ext;
                 }
                if(count($arrayfile)>0){
                    for($i=0; $i                        $image =$arrayfile[$i];
                        $uploadedfile = $tmp_file[$i];
                     if($galleryname[$i]!=""){
                        if ($image){
                            $filename = stripslashes($arrayfile[$i]);
                            $extension = getExtension($filename);
                            $extension = strtolower($extension);
                            if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
                                echo '

Unknown Image extension
';
                            }
else{

                                $size=filesize($tmp_file[$i]);
                                if ($size > MAX_SIZE*1024){
                                    echo '
You have exceeded the size limit! Image maximum size 1 Mb
';
                                }else{

if(!empty($tmp_file[$i])){
$info = getimagesize($tmp_file[$i]);

                                    if($extension=="jpg" || $extension=="jpeg" ){
                                        $uploadedfile = $tmp_file[$i];
if ($info['mime'] == 'image/jpeg')
                                        $src = imagecreatefromjpeg($uploadedfile);
                                    }else if($extension=="png"){
                                        $uploadedfile = $tmp_file[$i];
if($info['mime'] == 'image/png')
                                        $src = imagecreatefrompng($uploadedfile);
                                    }else{
if($info['mime'] == 'image/gif')
                                        $src = imagecreatefromgif($uploadedfile);
                                    }
                                    list($width,$height)=getimagesize($uploadedfile);
                                    $newwidth=784;
                                    $newheight=($height/$width)*$newwidth;
                                    $tmp=imagecreatetruecolor($newwidth,$newheight);
if(!empty($src)){
                                    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                                    $time=mktime(date("h"),date("i"),date("s"),date("d"));
                                    $filename = "../uploads/restaurant/".$time.$arrayfile[$i];
                                    imagejpeg($tmp,$filename,100);
                                    imagedestroy($src);
                                    imagedestroy($tmp);
                                    $data=array(
                                        'restaurant_name'=>$galleryname[$i],
                                        'restaurant_path'=>'uploads/restaurant/'.$time.$arrayfile[$i],
                                        'restaurant_status'=>$gl_status[$i],
'restaurant_desc'=>$content
                                    );
               $new_slide=new Model();
                                        $new_slide->Insert("tbadmin_restaurant", $data);
Image Uploaded Successfully!//Message
}
else{
Not real image file!//Message
}
}
else{
Your file is too big//Message
}
}
                                }
                            }else{
                                You do not choose file to upload//Message
                            }
                         }else{
                             You do not have enter restaurant name..!//Message
                        }
                    }
                }                  
}

No comments:

Post a Comment