在CodeIgniter中上传多个文件 [英] Upload multiple files in CodeIgniter

查看:147
本文介绍了在CodeIgniter中上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CodeIgniter项目中,我在项目创建期间上传文件。这是上传功能:

In my CodeIgniter project I'm uploading files during the project creation. Here's the uploading function:

function uploadFiles(){

     $this->load->library('upload');  
     $error = 0;    
     $projectName = $_POST['projectname'];
     mkdir('./uploads/'.$projectName);

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

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

       $config['upload_path']   = './uploads/'.$projectName;
       $config['allowed_types'] = 'xml|etl';
       $config['max_size']      = '0';
       $config['overwrite']     = TRUE;

      $this->upload->initialize($config);

      if($this->upload->do_upload())
      {
        $error += 0;
      }else{
        $error += 1;
      }
     }

     if($error > 0){ return FALSE; }else{ return TRUE; }

}

我在create_project函数中调用这个函数:

And I call this function in the create_project function like this:

public function create_project() {
    $this->load->library('form_validation');

    $this->form_validation->set_rules('projectname', 'Project name', 'required');

    $this->form_validation->set_message('required', 'This is an obligatory field');

    if($this->form_validation->run() == FALSE) {
        $this->load->view('project_creation_view');
    } else {
        $this->load->model('Project_management_model');
        $this->Project_management_model->create_project();
        $this->uploadFiles();
    }
}

但这不会做任何事情。文件未上传。甚至没有创建一个空目录。是否有人帮我找到错误?

However this does not do anything. The files are not being uploaded. Even an empty directory is not being created. Could anybody help me to find the error?

谢谢。

推荐答案

$config['upload_path'] = 'upload/Main_category_product/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);

foreach ($_FILES as $fieldname => $fileObject)  //fieldname is the form field name
{
    if (!empty($fileObject['name']))
    {
        $this->upload->initialize($config);
        if (!$this->upload->do_upload($fieldname))
        {
            $errors = $this->upload->display_errors();
            flashMsg($errors);
        }
        else
        {
             // Code After Files Upload Success GOES HERE
        }
    }
}

这篇关于在CodeIgniter中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆