2013-02-25 55 views
0

我使用下面的代码来处理图片上传。每件事情都很好。我允许选择上传两张图片,一张是徽标,另一张是截图。笨多张图片上传,让每个文件名

现在,当我提供编辑上传的选项时,比如选择新文件上传并替换旧文件,我遇到了这个问题。

在我的images[]数组中,我将借助循环逐个获取图像名称。然而,当用户想要改变第二图像(截图),并选择没有在标识领域,只有截图应该有所改变。

images[]阵列中,第一元件将从上传第一。如果我选择这两个图像是没事的,我会在images[0] logo.jpg和screenshot.jpg在images[1]。但是,当我只选择第二图像,我会在images[0]得到screenshot.jpg。但是,数组的第一个元素应该是空白的,并且数组的第二个元素应该具有第二个图像上传选项的数据,以使更改生效。我该如何实现这个目标?

$config['upload_path'] = '../uploads/'; 
      $config['allowed_types'] = '*'; 
      $config['max_size'] = '0'; 
      $config['max_width'] = '0'; 
      $config['max_height'] = '0'; 

      $this->load->library('upload', $config); 
      $image = array(); 
      foreach ($_FILES as $key => $value) { 

       if (!empty($value['tmp_name'])) { 

        if (! $this->upload->do_upload($key)) { 
         $error = array('error' => $this->upload->display_errors()); 
         //failed display the errors 
        } else { 
         //success 
         $data = $this->upload->data(); 
          $image[]= $data['file_name']; 
        } 

       } 
      } 

回答

3

请记住,您必须在每次上传时初始化上传库。

$this->load->library('upload'); 
$this->upload->initialize($config); 

用户指南:http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

编辑:

您可以检查$ _FILES数组,并检查是否有截图输入和标识输入的名称,如果他们aren' T,你可以创建一个数组,你需要

从来就发现这一点:

有创建的数据结构更简单的方法就是将其命名你的HTML文件输入不同的名称。 >如果您要上传多个文件,请使用:

<input type=file name=file1> 
<input type=file name=file2> 
<input type=file name=file3> 

每个字段的名称将是$ _FILES数组中的一个关键。

http://www.php.net/manual/es/features.file-upload.multiple.php#53933