简体   繁体   English

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

[英]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. 我正在使用Codeigniter,并且我知道有一个内置的文件上传类,但是我只是在尝试使用自定义的图像上传库。 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) 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;}
} 

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 . 您可以创建某种回滚功能并使用CI本机lib。 it's little extra work for the server but it's less/clean/easyTOdebug code and it works . 对于服务器来说,这几乎不需要额外的工作,但是它的代码更少/干净/ easyTOdebug,并且可以正常工作。

function do_upload()
{

1 - configuration 1-配置

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

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

2- uploading 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 - checking for errors and rollback at the end 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';
}




}

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_upload.php文件(定制库)中,特别是在此行中:

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

time() was perhaps overwriting the files. time()可能正在覆盖文件。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM