How to upload multiple images in php

1.4k Views Asked by At

I am developing a module of epaper in codeigniter(PyroCMS).
I want to know how can I upload multiple images ar once.

Can anyone guide me in uploading multiple images?

I tried but I only found code for uploading single image which I have already used in news module.

2

There are 2 best solutions below

0
On

In the view file give this code for image upload:

echo form_label('Multi Images','',$label_attr);
echo form_upload($multi_photo_attr);

where

$multi_photo_attr   =   array(
            'id'        =>  "cat_multi_images",
            'class'     =>  "multi",
            'name'      =>  "cat_multi_images[]",
            'maxlength' =>  "25",
            'multiple'  =>  "multiple"                      
    );

Now you need to create a folder in the root directory where your photos will be uploaded.

After that in the controller's method you need to store the path to that folder in a variable.This variable will be used to upload the images in the folder.

Next,get the names of all the images in a array something like this:

foreach($_FILES["cat_multi_images"] as $key => $value)
{
$i=0;
foreach($value as $key1 => $value1)
{
$multi_photo_array[$i][$key]    =   $value1;
$i++;
}

After that for every array element,i.e.,for every image run the below code to upload it:

function UploadFile($files,$path)
{   

    $extensions         =   array('jpeg','JPEG','gif','GIF','png','PNG','jpg','JPG','pdf','PDF','ZIP','zip','rar','RAR','html','HTML','TXT','txt','doc','docx','DOC','DOCX','ppt','PPT','pptx','PPTX','xlsx','XLSX','xls','XLS','exe','EXE','mp3','MP3','wav','WAV','m4r','M4R','mpeg','MPEG','mpg','MPG','mpe','MPE','mov','MOV','avi','AVI',);


    $destination        =   $path.$files["name"];
    //print_r($destination);exit;
    // GET FILE PARTS
    $fileParts          =   pathinfo($files['name']);
    $file_name          =   $files['name'];
    $file_name_only     =   $fileParts['filename'];
    $file_name_only     =   preg_replace('/[^a-zA-Z0-9]/','',$file_name_only);
    $file_extention     =   $fileParts['extension'];
    $Count              =   0;

    $destination        =   $path.$file_name_only.".$file_extention";
    $file_name          =   $file_name_only.".$file_extention";;

    // THIS SHOULD KEEP CHECKING UNTIL THE FILE DOESN'T EXISTS
    while( file_exists($destination))
    {
      $Count += 1;
      $destination  =  $path. $file_name_only."-".$Count.".$file_extention";
      $file_name    =  $file_name_only."-".$Count.".$file_extention";
    }

    $fileextension='';
    $filename='';
    if(!empty($files))
    {
        $filename=$files['name'];

        $fileextension=substr($filename,strpos($filename,".")+1);

        if(in_array($fileextension,$extensions))
        {

            $uploadstatus=move_uploaded_file($files["tmp_name"],$destination);
            if($uploadstatus)
            {
                return $file_name;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }   
    }       
}

Just copy the above code.It should work as it is made for a general case by me!You can copy that code in your model file and call it in the controller like this :

 $pr_photo_data = $this->admin_model->UploadFile($value,$targetPath_images);
$photo_list[]           =   $pr_photo_data;

And then store every image in the database

foreach($photo_list as $image)
{
$pro_image["cat_multi_images"]  =   $image;
$pro_retId =    $this->admin_model->add_multipic_cat($pro_image);
}

where

function add_multipic_cat($data) 
    {
        $retId = $this->database->query_insert("photo", $data);
        return $retId;
    }

Be careful.Take care and do every step accurately

0
On