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

时间:2012-10-12 21:22:05

标签: php codeigniter

我正在尝试上传多张图片。我正在使用Codeigniter,我知道有一个内置的文件上传类,但我只是在尝试我的自定义图像上传库。我提供了以下代码。

我面对以下代码的问题是它只是上传一个(表格中最后选择的那个)图像。

请你告诉我我做错了什么?

我的控制器:

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(自定义库)

  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;}
} 

我的表格

 <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 个答案:

答案 0 :(得分:0)

您可以创建某种回滚功能并使用CI本机库。它对服务器来说是一项额外的工作,但它更少/干净/ easyTOdebug代码,它的工作原理。

function do_upload()
{

1 - 配置

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

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

2-上传

        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 - 检查错误并在最后回滚

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




}

答案 1 :(得分:0)

我找到了问题的解决方案,并认为如果我分享它可以帮助别人。

问题出在image_upload.php文件(自定义库)中,特别是在这一行:

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

time()可能会覆盖文件。所以我用以下内容替换了我以前的代码:

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);
}