简体   繁体   中英

Trying to upload multiple images but it is uploading only one

I am trying to upload multiple images. I am using Codeigniter and I know there is a built in file upload class but I am just experimenting with my custom made image upload library. I have provided the code below.

The problem I am facing with the following code is it is just uploading only one (the one that is selected at last in the form) image.

Could you please kindly tell me where I am doing wrong?

My Controller:

function img_upload(){

   $this->load->library('image_upload');         

    $image1= $this->image_upload->upload_image('imagefile1');
    $image2= $this->image_upload->upload_image('imagefile2');
    $image3= $this->image_upload->upload_image('imagefile3');

   echo $image1 ."<br>"; echo $image2;  
}

application/libraries/image_upload.php (Custom made library)

  function upload_image($image_info){

    $image=$_FILES[$image_info]['name'];
    $filename = stripslashes($image);
    $extension = $this->getExtension($filename);
    $extension = strtolower($extension);

    $image_name=time().'.'.$extension;

   $newname="support/images/products/".$image_name;
   $uploaded = move_uploaded_file($_FILES[$image_info]['tmp_name'], $newname);

   if($uploaded) {return $image_name; } else {return FALSE;}
} 

My Form

 <form id="myForm" enctype="multipart/form-data" 
 action="<?php echo base_url();?>add/img_upload" method="post" name="myForm">

  <input type="file" name="imagefile1" size="20" /><br>
  <input type="file" name="imagefile2" size="20" /><br>
  <input type="file" name="imagefile3" size="20" /><br>
   <br /><br />
  <input type="submit" value="upload" />    
 </form>  

you can create some kind of rollback functionality and use CI native lib . it's little extra work for the server but it's less/clean/easyTOdebug code and it works .

function do_upload()
{

1 - configuration

    $config['upload_path'] = './uploads/';
    // other configurations 

    $this->load->library('upload', $config);
    $error = array('stat'=>0 , 'reason'=>'' ;

2- uploading

        if ( ! $this->upload->do_upload('file_1'))
        {
            $error['stat'] = 1 ;
            $error['reason'] = $this->upload->display_errors() ;

        }
        else
        { $uploaded_array[] = $uploaded_file_name ; } 

            // you may need to clean and re initialize library before new upload 
        if ( ! $this->upload->do_upload('file_2'))
        {
            $error['stat'] = 1 ;
            $error['reason'] = $this->upload->display_errors() ;

        }
        else
        { $uploaded_array[] = $uploaded_file_name ; } 

3 - checking for errors and rollback at the end

if($error['stat'] == 1 )
{   
    $upload_path = './upload/';
    if(!empty($uploaded_array))
    {
        foreach( $uploaded_array as $uploaded )
        { 
           $file = $upload_path.$uploaded;
           if(is_file($file))
           unlink($file);
        }
    }
    echo 'there was a problem : '.$error['reason'];
}
else
{
   echo 'success';
}




}

I have found the solution to my problem, and thought it could help someone if I share.

The problem was in the image_upload.php file (custom made library), specifically in this line:

 $image_name=time().'.'.$extension; // 

time() was perhaps overwriting the files. So I replaced my previous code with the following:

function upload_image($image_info){

     $image=$_FILES[$image_info]['name'];
     $filename = stripslashes($image);
     $extension = $this->getExtension($filename);
     $extension = strtolower($extension);

     $image_name=$this->get_random_number().'.'.$extension;

     $newname="support/images/products/".$image_name;
     $uploaded = move_uploaded_file($_FILES[$image_info]['tmp_name'], $newname);

  if($uploaded) {return $image_name; } else {return FALSE;}
 }


  function get_random_number(){

    $today = date('YmdHi');
    $startDate = date('YmdHi', strtotime('-10 days'));
    $range = $today - $startDate;
    $rand1 = rand(0, $range);
    $rand2 = rand(0, 600000);
    return  $value=($rand1+$rand2);
}

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