简体   繁体   中英

Uploading Multiple images via PHP

new code below. I use this oop PHP it was working I added this here because I deleted my older version of the question that was bad. if someone needs this kinda code is usable.

public function set_file($file) { 

        if(empty($file) || !$file || !is_array($file)) {
        $this->errors[] = "There was no file uploaded here";
        return false;
    
        }elseif($file['error'] !=0) {
    
        $this->errors[] = $this->upload_errors_array[$file['error']];
        return false;
    
        } else {
        $this->user_image =  basename($file['name']);
        $this->tmp_path = $file['tmp_name'];
        $this->type     = $file['type'];
        $this->size     = $file['size'];
    
    
        }
    }

public function move_image(){
        $target_path = SITE_ROOT.DS. 'admin' . DS . $this->upload_directory . DS . $this->user_image;
        return move_uploaded_file($this->tmp_path, $target_path);
      }

Try specify file's input name attribute as array:

<input type="file" id="gallery_album_image" multiple name="uploaded_file[]" >

and then you must refactor code work with $_FILES. This values will be arrays:

$_FILES["uploaded_file"]["name"][0..num of images]
$_FILES["uploaded_file"]["tmp_name"][0..num of images]

you can use this foreach loop too or anyone who needs.

// Configure upload directory and allowed file types 
$upload_dir = 'images_uploads'.DIRECTORY_SEPARATOR; 
$allowed_types = array('jpg', 'png', 'jpeg', 'gif'); 
  
// Define maxsize for files i.e 2MB 
$maxsize = 4 * 1024 * 1024;  
  
// Checks if user sent an empty form  
if(!empty(array_filter($_FILES['files']['name']))) { 
  
    // Loop through each file in files[] array 
    foreach ($_FILES['files']['tmp_name'] as $key => $value) { 
          
        $file_tmpname = $_FILES['files']['tmp_name'][$key]; 
        $file_name = $_FILES['files']['name'][$key]; 
        $file_size = $_FILES['files']['size'][$key]; 
        $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); 
        $file_name = time().rand().  $file_name.".".$file_ext;
        // Set upload file path 
        $filepath = $upload_dir.$file_name; 

  $sql = "INSERT INTO gallery_gallery(gallery_album_id, gallery_images) VALUES(:gallery_album_id,:file_name) "; 
         
  $values = [
           ':gallery_album_id' => $gallery_album_id, 
           ':file_name' => $file_name
          ];
    
        $query =$connection->prepare($sql);
        $query->execute($values);
  
        // Check file type is allowed or not 
        if(in_array(strtolower($file_ext), $allowed_types)) { 
  
            // Verify file size - 2MB max  
            if ($file_size > $maxsize)          
                echo "Error: File size is larger than the allowed limit.";  
  
            // If file with name already exist then append time in 
            // front of name of the file to avoid overwriting of file 
            if(file_exists($filepath)) { 
                $filepath = $upload_dir.time().$file_name; 
                  
                if( move_uploaded_file($file_tmpname, $filepath)) { 
                    echo "{$file_name} successfully uploaded <br />"; 
                }  
                else {                      
                    echo "Error uploading {$file_name} <br />";  
                } 
            } 
            else { 
              
                if( move_uploaded_file($file_tmpname, $filepath)) { 
                    echo "{$file_name} successfully uploaded <br />"; 
                } 
                else {                      
                    echo "Error uploading {$file_name} <br />";  
                } 
            } 
        } 
        else { 
              
            // If file extention not valid 
            echo "Error uploading {$file_name} ";  
            echo "({$file_ext} file type is not allowed)<br / >"; 
        }  
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM