douyanjing8287 2012-10-12 21:22
浏览 72

尝试上传多张图片,但只上传一张

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>  
  • 写回答

2条回答 默认 最新

  • duanan1228 2012-10-13 07:04
    关注

    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';
    }
    
    
    
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)