如何在codeigniter中添加多个文件

时间:2015-05-21 18:19:46

标签: php 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个图像,则意味着只添加和上传了一个图像。剩下的两张图片没有上传。我不知道我在哪里做错了。

1 个答案:

答案 0 :(得分:0)

你的道路是否正确?

'upload_path' => 'charity_gallery',

这个文件夹在哪里?请尝试使用./charity_gallery/。 (确保也拥有正确的权限)