多个图像上传问题

时间:2018-04-19 08:49:08

标签: php jquery ajax

我很累,我正在尝试上传多张图片,但这些代码在我的数据库中插入单张图片请任何正文帮助我,我的代码有什么问题,谢谢提前 我的控件

function blog_img(){
    $this->load->library('upload');

    $files = $_FILES;
    $count = count($_FILES['userfile']['name']);
    // Faking upload calls to $_FILE
    for ($i = 0; $i < $count; $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];

      $config = array(
        //'file_name'     => <your ouw function to generate random names>,
        'allowed_types' => 'gif|jpg|png',
        'max_size'      => 3000,
        'overwrite'     => FALSE,

        /* real path to upload folder ALWAYS */
        'upload_path'
            => './photo/uploads',
      );

    $this->upload->initialize($config);
    $this->upload->do_upload('userfile');

    $file_name[] = $this->upload->data();

    $data = array(

        'userfile'   => $this->upload->data('file_name'),

    );

    $this->blog->blog_img($data);
            redirect('/admin/blog/img/new');
    endfor;
}

2 个答案:

答案 0 :(得分:1)

Here is the solution 

function blog_img(){

    $number_of_file = sizeof($_FILES['userfile']['tmp_name']);
    $file = $_FILES['userfile'];

    // Faking upload calls to $_FILE
    for ($i = 0; $i < $number_of_file; $i++) :

      $_FILES['userfile']['name']     = $file ['name'][$i];
      $_FILES['userfile']['type']     = $file ['type'][$i];
      $_FILES['userfile']['tmp_name'] = $file ['tmp_name'][$i];
      $_FILES['userfile']['error']    = $file ['error'][$i];
      $_FILES['userfile']['size']     = $file ['size'][$i];

     $config['upload_path'] = './photo/uploads'; //The path where the image will be save
    $config['allowed_types'] = 'gif|jpg|png';
   $this->load->library('upload', $config);

    $this->upload->initialize($config);
    $this->upload->do_upload('userfile');
    $data = $this->upload->data();
    $file_name[] = $this->upload->data();
    $data = array(

        'userfile'   => $this->upload->data('file_name'),

    );

    $this->blog->blog_img($data);
    //redirect('/admin/blog/img/new');
    endfor;
        }

答案 1 :(得分:0)

你犯了一个错误,

只需删除userfile,如下所示:

$this->upload->do_upload();

而不是

$this->upload->do_upload('userfile');

在代码开始之前使用库$this->load->library('upload');

确保您的html应该像

<input type="file" name="userfile[]" multiple="multiple">