【问题标题】:[Codeigniter]Multiple upload cant insert to database and not uploading to directory[Codeigniter]多次上传不能插入数据库,不能上传到目录
【发布时间】:2019-09-22 13:47:52
【问题描述】:

如何使用 Codeigniter 4.x 上传多张图片?

我尝试在我的模型中循环 do_upload 函数,但它不起作用,我尝试了从 GitHub 获得的另一个代码,但仍然没有任何效果。

这是我的看法:

<div class="section-body">
        <h2 class="section-title">Entry Group Passanger</h2>
        <div class="row">
            <div class="col-md-12">
                <form action="<?= base_url('passanger/addgroup') ?>" method="POST" enctype="multipart/form-data">

                <?php for($i=1;$i<=$pax['pax'];$i++) : ?>

                <h5>Passanger ke-<?= $i ?></h5>

                    <div class="form-row">
                        <div class="col-md-6">
                            <label for="fullname">Fullname</label>
                            <input type="text" class="form-control" name="name[]">
                            <?= form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
                        </div>
                        <div class="col-md-6">
                            <label for="gender">Gender</label>
                            <select class="form-control select2-input" name="gender">
                                <option>-- Select Gender --</option>
                                <option value="Male">Male</option>
                                <option value="Female">Female</option>
                                <option value="Other">Other</option>
                            </select>
                            <?= form_error('gender', '<small class="text-danger pl-3">', '</small>'); ?>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="col-md-12 mb-4 mt-2">
                            <label for="">Upload Passport</label>
                            <input type="file" name="upload_passport[]" id="input-file-now" class="dropify" />
                        </div>
                    </div>

这是我的模型:

public function addGroup() 
{
    $user = $this->db->get_where('user', ['username' => $this->session->userdata('username')])->row_array();
    $pax = $this->getPaxById();
    date_default_timezone_set('Asia/Jakarta');

    $upload_image = $_FILES['upload_passport[]']['name'];

    if ($upload_image) {
        $config['upload_path'] = './assets/img/uploads/passport';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']     = '2048';

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

        for($i=0;$i<$pax['pax'];$i++) {
            if ($this->upload->do_upload('upload_passport[$i]')) {

                $new_image = $this->upload->data('file_name');

            } else {
                echo $this->upload->display_errors();
            }
        }
    }

    $name = $this->input->post("name[]");
    $gender = $this->input->post("gender[]");
    $birthdate = $this->input->post("birthdate[]");
    $no_passport = $this->input->post("no_passport[]");
    $expire_passport = $this->input->post("expire_passport[]");
    $destination = $this->input->post("destination[]");
    $date = $this->input->post("date[]");
    $dp1 = "0";
    $dp2 = "0";
    $lunas = "0";
    $type = "Group";
    $status = "Not Complete";
    $permission = "0";
    $upload_passport = $new_image;
    $upload_dp1 = "default.png";
    $upload_dp2 = "default.png";
    $date_created = date('d/m/Y H:i:s');
    $data = [];
    $jumlah = $pax['pax'];

    for($x=0;$x<$jumlah;$x++) {

        array_push($data, [
            'name'=>$this->input->post("name[$x]"),
            'id_user'=> $user['id_user'], 
            'gender'=>$this->input->post("gender[$x]"),  
            'birthdate'=>$this->input->post("birthdate[$x]"), 
            'no_passport'=> $this->input->post("no_passport[$x]"),
            'expire_passport'=> $this->input->post("expire_passport[$x]"),
            'destination'=> $this->input->post("destination[$x]"),
            'date'=> $this->input->post("date[$x]"),
            'dp1'=> $dp1,
            'dp2'=> $dp2,
            'lunas'=> $lunas,
            'pax'=> $pax['pax'],
            'type'=> $type,
            'status'=> $status,
            'permission'=> $permission,
            'upload_passport'=> $new_image,
            'upload_dp1'=> $upload_dp1,
            'upload_dp2'=> $upload_dp2,
            'date_created'=> $date_created
        ]);
        var_dump($data); die;
    }
    $this->db->insert_batch("passanger", $data);

}

我想要得到的是:

array('','','upload_image'), array('','','upload_image')

当将它插入数据库时​​,但当我这样做时,上传只是得到输出null,它甚至没有上传到我的上传目录。如果您能帮助我,我将不胜感激,我已经坚持了 3 天。谢谢。

【问题讨论】:

标签: codeigniter file-upload error-handling


【解决方案1】:
for ($i=0; $i < sizeof($_FILES['image']['name']) ; $i++) { 
                    $_FILES['file']['name']     = $_FILES['image']['name'][$i];
                    $_FILES['file']['type']     = $_FILES['image']['type'][$i];
                    $_FILES['file']['tmp_name'] = $_FILES['image']['tmp_name'[$i];
                    $_FILES['file']['error']    = $_FILES['image']['error'][$i];
                    $_FILES['file']['size']     = $_FILES['image']['size'][$i];
                    $config['upload_path'] = PRODUCT_IMAGE_UPLOAD_PATH;
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
                    $config['encrypt_name']  = TRUE;
                    $this->load->library('upload', $config);
                    if ($this->upload->do_upload('file')){
                        $images[] = $this->upload->data('file_name');
                    }else {
                        echo $this->upload->display_errors();
                    }
                }

【讨论】:

    【解决方案2】:

    在 Codeigniter 4 中试试这个

    在你的控制器中添加这个

    $image_model = new App\Models\Images(); // Your model name that is load your model
    if($imagefiles = $this->request->getFiles())
    {
        foreach($imagefiles['uploadImages'] as $img)
        {
            if ($img->isValid() && ! $img->hasMoved())
            {
                $image_name = $img->getRandomName(); // This is applicable if you want to change the name of the images
    
                if($image_model->save_images($image_name))
                {
                    $img->move(WRITEPATH.'uploads/images/ads', $image_name); // WRITEPATH.'uploads/images/ads' is the part you wish to save you file to
                }
            }
        }
    }
    


    在您的模型中,例如图像模型,我有一个方法调用 save_images()as this

    use Codeigniter\Model
    
    class Images extends Model{
       public function save_images($image = '')
       {
           // We set the column 'name' to be allow for data entry into the db. note this is mandatory for now.
          // hope it will change in feature. 'name' is the column in database table where you will save the image name
          // this command the CI4 db to select only the image column in the database table
           $this->allowedFields = ['name'];
    
           return $this->db->table('images')->insert(['name' => $image]);
          // echo $sql = $this->db->getLastQuery();
        }
    }
    

    然后在你的html文件输入中,这样做

    <form action="" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <input type="file" name="uploadImages[]" multiple>
        </div>
    </form>
    


    我希望这对你有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多