2015-05-21 25 views
0

我在我的项目中使用codeigniter,我在将多个文件上载到数据库时遇到了一个问题。上传的图像没有移动到特定的文件夹。所以我的文件没有保存在我的数据库中。我在下面附上我的代码,请找到下面的附件,并告诉我我错过了什么。 在此先感谢。 型号:如何在codeigniter中添加多个文件

public function myfunction(){ 
$config = array(
      'upload_path' => 'charity_gallery', 
      'max_size' => 2000 * 2000, 
      'allowed_types' => 'gif|jpeg|jpg|png', 
      'overwrite' => true, 
      'remove_spaces' => true); 
    $images = array(); 
    $this->load->library('upload', $config); 

    $files = $_FILES; 
    $count = count($_FILES['charity_image']['name']); 
    echo $count; 

    for ($i = 0; $i < $count; $i++) { 

     $_FILES['charity_image']['name'] = $files['charity_image']['name'][$i]; 
     $_FILES['charity_image']['type'] = $files['charity_image']['type'][$i]; 
     $_FILES['charity_image']['tmp_name'] = $files['charity_image']['tmp_name'][$i]; 
     $_FILES['charity_image']['error'] = $files['charity_image']['error'][$i]; 
     $_FILES['charity_image']['size'] = $files['charity_image']['size'][$i]; 

     $fileName = $_FILES['charity_image']['name']; 
     $images[] = $fileName; 
     $config['file_name'] = $fileName; 
     $imgName = 'charity_gallery/'.$fileName; 
     //echo $imgName; 
     $this->upload->initialize($config); 
     //echo "hello"."INSERT INTO `charity_gallery` (charity_id, image_name, created_on) VALUES ('$cid', '$imgName', '$createdOn')"; 
     if ($this->upload->do_upload('charity_image')) { 
      echo "how"."INSERT INTO `charity_gallery` (charity_id, image_name, created_on) VALUES ('$cid', '$imgName', '$createdOn')"; 
      $query = $this->db->query("INSERT INTO `charity_gallery` (charity_id, image_name, created_on) VALUES ('$cid', '$imgName', '$createdOn')"); 

     } 


    } 
    $aff = $this->db->affected_rows(); 
    return $aff; 
} 

HTML:

<div class="control-group" style="padding:15px;"> 
           <label class="control-label" for="basicinput" style="padding:10px;">Image Gallery:</label> 
          <div class="controls"> 
           <div class="input-append span6"> 
            <input type="file" class="span12" placeholder="Upload file" id="charity_image" name="charity_image[]" multiple> 
           </div> 
           <div class="span12" style="margin-top: 15px;"> 
            <output id="result" /> 
           </div> 
          </div> 
          </div> 

在此,如果我添加完全3图像装置获取添加仅一个图像和上传。剩下的两张图片没有上传。我不知道我在做错什么。

+0

您是否收到任何错误? – dansasu11

+0

不,我没有得到任何错误..... – Vijaykarthik

回答

0

你的路径是否正确?

'upload_path' => 'charity_gallery',

凡位于此文件夹?改为尝试./charity_gallery/。 (请确保也有正确的权限)

+0

是'charity_gallery'是我的路径。我的代码是正确的?.... – Vijaykarthik

+0

如果我添加共3张图片意味着只有1张图片被添加并上传。剩下的两张图片没有上传。 – Vijaykarthik

+0

先尝试不插入到DB ....只是为了测试上传部分是否正确。 – jfreites

相关问题