Reduce size of multiple uploaded images I am a young programmer with the ability to learn more. I created a public site where a user can upload multiple pictures at once. The code works successfully and inserts the images into a MySQL database. As I improved the code further by reducing the size of multiple uploaded pictures at once, only one picture was uploaded into the folder and MySQL. Could anyone please help me on this? Is there any style or code to reduce the size of multiple images uploaded by the user?

DS in my code is DIRECTORY_SEPARATOR define in initialize file.

if(isset($_POST['submitImage']))
{
    $file = $_FILES['upload'];
    $errors = array();

    if(empty($file))
    {
        $errors[] = ' The file could not be empty ';
    }
    else
    {
        foreach( $file['name'] as $key =&gt; $value )
        {
            $extensions = array("jpeg","jpg","png"); 
            $file_ext=explode('.',$file['name'][$key]);
            $file_ext=end($file_ext);
            //$file_ext=strtolower(end(explode('.',$file['name'][$key])));

            if(in_array($file_ext,$extensions ) === false)
            {
                $errors[] = "extension not allowed";
            }
            else
            {
                $filetmp = $file['tmp_name'][$key];
                $terget_path = SITE_ROOT.'j2reimage'.DS.'image'.DS;
                $dat = strftime("%Y-%m-%d %H:%M:%S", time());

                if(file_exists($terget_path))
                {
                    //move_uploaded_file( $filetmp, $terget_path.'/'.$file['name'][$key]);              
                    mysql_query("insert into 4j2memberimage values ('', $memmem-&gt;id,'{$file['name'][$key]}', '$dat')"); 

                    if(move_uploaded_file($filetmp, $terget_path.'/'.$file['name'][$key] ))
                    {                   
                        $exe = explode(".", $file['name'][$key]);
                        $ext = $exe[1];
                        $w = 250;
                        $h = 250;
                        $target  = $terget_path.DS.$file['name'][$key];                 
                        $newcopy = SITE_ROOT.'j2reimage'.DS.'reCopy'.DS.$file['name'][$key];
                        list($w_orig, $h_orig) = getimagesize($target);
                        $scale_ratio = $w_orig / $h_orig;

                        if (($w / $h) &gt; $scale_ratio)
                        {
                            $w = $h * $scale_ratio;
                        }
                        else
                        {
                            $h = $w / $scale_ratio;
                        }

                        $img = "";
                        $ext = strtolower($ext);

                        if ($ext == "gif")
                        { 
                            $img = imagecreatefromgif($target);
                        }
                        else if($ext =="png")
                        { 
                            $img = imagecreatefrompng($target);
                        }
                        else
                        {
                            $img = imagecreatefromjpeg($target);
                        }

                        $tci = imagecreatetruecolor($w, $h);
                        imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);

                        if(@imagejpeg($tci, $newcopy, 80))
                        {
                            //return true;
                            $terger_path = $target; 
                            //$terger_path = SITE_ROOT.DS.$this-&gt;img_path.DS.$this-&gt;filename;
                            return unlink($terger_path) ? true : false;                     
                        }
                    }
                    //end of if file_exists     
                }
                else
                {
                    $errors[] = 'Upload extention could not be located';            
                }
            }
            //redirect_to('inb.php');                   
        }
    }
}
In your HTML View you can use this
&lt;file name="upload[]" accept="image/*" multiple&gt; multiple and accept attribute is only available in HTML5 dosen't work in old browsers.

&lt;?php
    $files = null;
    $errors = null;

    define('IMAGE_TARGET',SITE_ROOT.'j2reimage'.DS.'image');

    if(!empty($_POST['submitImage']))
    {
        $files = $_FILES['upload'];
        $errors = array();

        if(!empty($files))
        {
            //Move extension list to here you don't need redeclarate variables inside loop
            $valid_extensions = array('jpeg','jpg','png','gif'); 

            foreach($files as $file)
            {

                    //Don't use explode for extensions
                    //$file_ext = explode('.',$file['name'][$key]);
                    //Use pathinfo
                    $path_parts = pathinfo($file);                
                    $file_ext = $path_parts['extension'];

                    //$file_ext=strtolower(end(explode('.',$file['name'][$key])));

                    if(in_array($file_ext,$valid_extensions) === false)
                    {
                        $errors[] = ".$file_ext extension not allowed";
                    }
                    else
                    {
                        $filetmp = $file['tmp_name'][$key];

                        $dat = strftime("%Y-%m-%d %H:%M:%S", time());

                        if(file_exists(IMAGE_TARGET.DS.$file['name']))
                        {
                            //move_uploaded_file( $filetmp, $terget_path.'/'.$file['name'][$key]);              
                            mysql_query("INSERT INTO 4j2memberimage VALUES ('', $memmem-&gt;id,'{$file['name']}', '$dat')"); 

                            //if(move_uploaded_file($filetmp, $terget_path.'/'.$file['name'][$key] ))

                            //Use copy insteds of  move_uploaded_file sometimes you have right problems in some servers.

                            if(copy($filetmp, IMAGE_TARGET.DS.$file['name'] ))
                            {                   
                                $exe = explode(".", $file['name']);
                                $ext = $exe[1];
                                $w = 250;
                                $h = 250;
                                $target  = IMAGE_TARGET.DS.$file['name'];                 
                                $newcopy = IMAGE_TARGET.DS.'thumb_'.DS.$file['name'];

                                list($w_orig, $h_orig) = getimagesize($target);
                                $scale_ratio = $w_orig / $h_orig;

                                if (($w / $h) &gt; $scale_ratio)
                                {
                                    $w = $h * $scale_ratio;
                                }
                                else
                                {
                                    $h = $w / $scale_ratio;
                                }

                                $img = '';
                                $ext = strtolower($ext);

                                switch($ext) {
                                    case 'gif':
                                        $img = imagecreatefromgif($target); 
                                        break;
                                    case 'png':
                                        $img = imagecreatefrompng($target); 
                                        break;
                                    case 'jpeg':
                                    case 'jpg':
                                        $img = imagecreatefromjpeg($target);
                                        break;                                                              
                                }

                                $tci = imagecreatetruecolor($w, $h);
                                imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);

                                if(@imagejpeg($tci, $newcopy, 80))
                                {
                                    //return true;
                                    $target_path = $target; 
                                    //$terger_path = SITE_ROOT.DS.$this-&gt;img_path.DS.$this-&gt;filename;
                                    file_put_contents($target_path,$img);
                                    //return unlink($target_path) ? true : false;                     
                                }
                            }
                            //end of if file_exists     
                        }
                        else
                        {
                            $errors[] = 'Upload extention could not be located';            
                        }
                    }
                    //redirect_to('inb.php');                   
                }//foreach      
        }
        else
        {
            $errors[] = ' The file could not be empty ';            
        }//if
    }//if