php - 在 CodeIgniter 中上传多个文件

标签 php codeigniter file-upload

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

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 函数中这样调用这个函数:

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();
    }
}

然而这并没有做任何事情。文件未上传。甚至不创建空目录。谁能帮我找出错误?

谢谢。

最佳答案

您可以上传任意数量的文件..

$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
        }
    }
}

关于php - 在 CodeIgniter 中上传多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8377218/

相关文章:

php - 反序列化mysql表中的数据并通过php输出?

PHP 自动递增文件名

拉拉维尔 5.4 : hasFile() no longer works

PhP mysql插入语法错误

php - 从Youtube Channel获取视频信息,使用PHP将其存储在MYSQL中

php - Mysql插入时出现重复条目​​错误

php - Codeigniter + ajax(jQuery) session 问题

mysql - Codeigniter 时区 mysql 设置

php - 如何使用 codeigniter 中的事务根据数据更改进行提交和回滚?

apache - 如何在 HTTP 中处理文件上传?