Welcome Guest, Not a member yet? Register   Sign In
Optimization and best practice for multiple image upload
#1

(This post was last modified: 09-09-2018, 07:55 AM by xenos92.)

Hello,
My code is a multiple image download processing code called ajax. 
It is functional but I wonder if there was no way to go or optimize a little? 
If there are better ways to write the code, do not hesitate to say it to progress, thank youWink



PHP Code:
function ajax_preview_image(){

    
sleep(3);

    if(!empty(
$_FILES['images'])){

        
// Configuration
        
define('KB'1024);
        
define('MB'1048576);
        
define('GB'1073741824);
        
define('TB'1099511627776);
        
$folder                 'asset/img/tmp/';
 
       $allowed_types             'jpg|JPG|jpeg|JPEG|png|PNG';
 
       $max_size                 1*MB;
 
       $max_filename              '255';
        
$files                     $_FILES;
        
$count_files_selected     count($files['images']['name']);
        
$json = array();
        
        
Dossier::create_folder($folder); // I create the temporary folder if it does not exist
     
   Dossier::supprimer_all_fichier($folder); // I empty it to fill a new folder

        // I change the $ _FILES array structure to be cleaner
     
   $array_img $files['images'];

     
   function clear_image_name($image_name,$key){
     
       $path_parts pathinfo($image_name);
 
           $filename $path_parts['filename'];
 
           $extension $path_parts['extension'];
            
$image_name_clear 'image_'.$key.'.'.$extension;
            return 
$image_name_clear;
     
   }

        
$new_array_img = array();
        foreach (
$array_img as $key => $element) {
            foreach (
$element as $j => $sub_element) {
                
$new_array_img[$j][$key]                 = $sub_element;
                
$new_array_img[$j]['upload_statut']         = false;
                
$new_array_img[$j]['messages_error']    = null;

                
// I clean the name
                
$new_array_img[$j]['name']                = clear_image_name($new_array_img[$j]['name'],$j);
            }
        }

        
// I make my checks on all the images posted to sort and update the table new_array_img
        
for($i=0$i<$count_files_selected$i++){ 
     
       $path_parts pathinfo($new_array_img[$i]['name']);
     
       $name_image $path_parts['filename'];
     
       $extension_image $path_parts['extension'];
            
$valid_max_size         false;
            
$valid_max_filename     false;
            
$valid_allowed_types     false;

            
// Size
     
       if($new_array_img[$i]['size'] <= $max_size){
     
           $valid_max_size true;
     
       }
     
       else{
     
           $error = array('size'=>'image trop lourde');
     
           $new_array_img[$i]['messages_error']    = $error;
     
       }
     
       
            
// Extension
     
       $allowed_types_array  explode("|"$allowed_types);
            if(
in_array($extension_image$allowed_types_array)){
             
   $valid_allowed_types true;
            }
            else{
     
           $error = array('extension'=>'Extension non prise en compte');
     
           $new_array_img[$i]['messages_error']    = $error;
     
       }
     
       // Validation
     
       if($valid_max_size == true && $valid_allowed_types == true){
     
           $new_array_img[$i]['upload_statut']     = true;
     
       }
     
   }

        function 
removeElementWithValue($array$key$value){
         
    foreach($array as $subKey => $subArray){
         
         if($subArray[$key] == $value){
         
              unset($array[$subKey]);
         
         }
         
    }
         
    return $array;
        }

        
// I create a table with the answers that do not have the status upload to false
        
$valid_array = array();
        
$unset_array_valid removeElementWithValue($new_array_img"upload_statut"false); 
        
$valid_array array_values($unset_array_valid); // Je reindexe le tableau car j'ai supprimer des valeurs

        // I create a table with the answers that do not have the status upload to true
        
$false_array = array();
        
$unset_array_false removeElementWithValue($new_array_img"upload_statut"true); 
        
$false_array array_values($unset_array_false); // Je reindexe le tableau car j'ai supprimer des valeurs

        // I'm doing a loop of my "valid_array" array to upload the images
     
   foreach($valid_array as $valid_img){           
            $_FILES 
= array(); // I initialize the $ _FILES that will be transmitted for the upload
     
       $_FILES['images'] = $valid_img;

     
       $config = array(
     
           'upload_path'   => $folder,
     
           'allowed_types' => $allowed_types,
     
              'encrypt_name'  => FALSE,
     
              'max_size'        => $max_size,
     
           'overwrite'     => TRUE,
     
           'max_filename'  => $max_filename,
     
       );

     
       // I do the upload
     
       $this->load->library('upload',$config);
     
       $this->upload->initialize($config);
            
$this->upload->do_upload('images');
 
       }

 
       // I return the data to display the thumbnails. I have to compare them to the upload folder to verify that everyone is uploader
        
$exclude_folders = array('..''.''min','error');
        
$json['image_uploaded_folder'] = array_values(array_diff(scandir($folder), $exclude_folders));

        
$i 0;
        foreach(
$valid_array as $valid_img){
     
       $json['valid_preview_html'][$i]['name'] = '<div>'.$valid_img['name'].'</div>';

            if (
in_array($valid_img['name'], $json['image_uploaded_folder'])){
                
$json['valid_preview_html'][$i]['message'] = '<div>Statut : Uploader </div>';
                
$json['valid_preview_html'][$i]['miniature'] = '<img src="'.Image::crop($folder.$valid_img['name'],150,150).'" >';
            }
            else{
                
$json['valid_preview_html'][$i]['message'] = "Une erreur s'est produite, veuillez recommencer";
                
$json['valid_preview_html'][$i]['miniature'] = '<div> Erreur </div>';
            }

            
$html_preview '';
            
$html_preview .= '    <div class="image_preview">
                                    <div class="img_preview">'
.$json['valid_preview_html'][$i]['miniature'].'</div>
                                       <div class="column_2">
                                           <div class="img_name" >'
.$json['valid_preview_html'][$i]['name'].'</div>
                                           <div class="img_message">'
.$json['valid_preview_html'][$i]['message'].'</div>
                                           <div class="ajax_preview_delete_button" data-name="'
.$valid_img['name'].'">Supprimer</div>
                                       </div>
                                   </div>'
;
            
$json['html_valid'][$i] = $html_preview;
            
$i++;
        }

        
$i=0;
        foreach(
$false_array as $false_img){
     
       $json['false_preview_html'][$i]['name'] = '<div>'.$false_img['name'].'</div>';
     
       $json['false_preview_html'][$i]['message'] = "Erreur";
            
$json['false_preview_html'][$i]['miniature'] = '<div> Erreur </div>';

            
$html_preview '';
            
$html_preview .= '    <div class="image_preview">
                                    <div class="img_preview">'
.$json['false_preview_html'][$i]['miniature'].'</div>
                                       <div class="column_2">
                                           <div class="img_name" >'
.$json['false_preview_html'][$i]['name'].'</div>
                                           <div class="img_message">'
.$json['false_preview_html'][$i]['message'].'</div>
                                       </div>
                                   </div>'
;
            
$json['html_error'][$i] = $html_preview;
            
$i++;
        }

        
$json['false'] = $false_array;
     
   $json['new_array_img'] = $new_array_img;
     
   echo json_encode($json);
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB