0

我正在尝试上传多张图片。我正在使用 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>  
4

2 回答 2

0

您可以创建某种回滚功能并使用 CI 本机 lib 。这对服务器来说几乎没有额外的工作,但它的代码更少/干净/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';
}




}
于 2012-10-13T07:04:57.137 回答
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);
}
于 2012-10-13T08:15:13.610 回答