php - 使用 Codeigniter 上传多张图片,仅将一个文件路径保存到 MySQL 数据库

标签 php mysql codeigniter

现在我正在构建一个应用程序。我已经设置了一个表单,可以将多个图像上传到数据库。这是我的简单代码

查看

<?php echo form_open_multipart('admin/product/post'); ?>
<table class="table table-stripped">
    <tbody>
        <tr>
            <td>Code</td>
            <td>
                <?php echo form_input(array('class'=>'form-control','name'=>'kodeproduk')); ?>                                  
            </td>
        </tr>
        <tr>
            <td>Display</td>
            <td>
                <input class="form-control" type="file" name="userfile[]" id="multiple" multiple="" />    
            </td>
        </tr>
        <tr>
            <td>Description</td>
            <td>
                <div class="textarea textarea-editor">
                    <textarea name="ket" cols="50" rows="5" class="form-control"></textarea>
                </div>                                  
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <button type="submit" class="btn btn-primary btn-outline btn-block pull-right"><span>Save</span></button>
            </td>
        </tr>
    </tbody>
</table>
<?php echo form_close(); ?>

Controller

public function post(){        
    if($this->_validation()===FALSE){ 
        $this->session->set_flashdata('error', 'Ooops, there was an error');
        redirect(base_url("admin/product"));
    }else{
        $files = $_FILES;
        $cpt = count($_FILES['userfile']['name']);
            for($i=0; $i<$cpt; $i++){
            $_FILES['userfile']['name']= $files['userfile']['name'][$i];
            $_FILES['userfile']['type']= $files['userfile']['type'][$i];
            $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
            $_FILES['userfile']['error']= $files['userfile']['error'][$i];
            $_FILES['userfile']['size']= $files['userfile']['size'][$i];
            $this->upload->initialize($this->set_upload_options());
            $this->upload->do_upload();
            $fileName = $_FILES['userfile']['name'];
            $images[] = $fileName;
        }
        $fileName = implode(',',$images);

        $data = array(  'kodeProduk'                => $this->input->post('kodeproduk'),
                        'ket'                       => $this->input->post('ket'),

                        'GambarBesar'               => $fileName
        );

        unset($data['submit']);                             
        $this->table->add_record($data);
        $this->session->set_flashdata('success', 'Product has been saved.');
        redirect(base_url("admin/product"));
    }   
}

型号

public function add_record($data){
    $this->db->insert('produk', $data);
    return;
}

我在发布时遇到问题,所有图像文件都上传到服务器上的目录,但实际上只有一个图像作为一行存储到 MySQL 表中。那么如何修复我的代码呢?提前致谢

最佳答案

好的,一些小的改变可能会有所帮助

public function post(){        
    if($this->_validation()===FALSE){ 
        $this->session->set_flashdata('error', 'Ooops, there was an error');
        redirect(base_url("admin/product"));
    }else{
        $files = $_FILES;
        $images = array();
        $cpt = count($_FILES['userfile']['name']);
            for($i=0; $i<$cpt; $i++){
            $_FILES['userfile']['name']= $files['userfile']['name'][$i];
            $_FILES['userfile']['type']= $files['userfile']['type'][$i];
            $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
            $_FILES['userfile']['error']= $files['userfile']['error'][$i];
            $_FILES['userfile']['size']= $files['userfile']['size'][$i];
            $this->upload->initialize($this->set_upload_options());
            $this->upload->do_upload();
            $images[] = $_FILES['userfile']['name'];
        }
        $fileName = implode(',',$images);

        $data = array(  'kodeProduk'                => $this->input->post('kodeproduk'),
                        'ket'                       => $this->input->post('ket'),

                        'GambarBesar'               => $fileName
        );

        unset($data['submit']);                             
        $this->table->add_record($data);
        $this->session->set_flashdata('success', 'Product has been saved.');
        redirect(base_url("admin/product"));
    }   
}

关于php - 使用 Codeigniter 上传多张图片,仅将一个文件路径保存到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35471811/

相关文章:

php - 如何检索 WHERE 子句中使用数组的数据?

php - Codeigniter - 如何将数据库结果垂直显示为列而不是行

codeigniter - $this->load->model() 在 CodeIgniter 中不起作用

php - 如何支持网站匿名?

php - 如何在Mysql中分页查询中获取5行以内的特定id

php - PHP获取YouTube标题并显示它

.htaccess - 文件/路径不带尾部斜杠的规范 URL

php - Opera 字符集 UTF-8

mysql - 使用 NULL 值连接

mysql - 有没有办法计算 MySQL 查询响应的大小?