【问题标题】:How to upload files using codeigniter hmvc如何使用 codeigniter hmvc 上传文件
【发布时间】:2017-02-24 09:10:38
【问题描述】:

请帮忙!我使用的是 codeigniter 3 mvc,我的代码工作得很好。然后我搬到了 hmvc,我的上传不再起作用。以下是我的代码:

class Home extends MY_Controller
{ 
  function __construct()
 {

parent::__construct();
$this->load->model('User_m', 'user');
$this->load->library('upload');

}



public function register()

{

$data['services'] = $this->user->get_service_list();
$data['page_class'] = 'register-page';
$data['title'] = 'Registration';
$data['content_view'] = 'Home/register_v';
$this->template->login_template($data);

}



public function apply()

{

 $directoryname = $this->input->post('company').'/documents';

 if (!is_dir('customer_documents/'.$directoryname)) 
  {
   mkdir('./customer_documents/' . $this->input->post('name_of_org').'/documents', 0777, TRUE);

  }

      //Field Rules

        $this->form_validation->set_rules('company', 'Company/', 'trim|required|min_length[6]');
        $this->form_validation->set_rules('address', 'Physical Address', 'trim|required');
        $this->form_validation->set_rules('tel', 'Tel', 'trim|required');
        $this->form_validation->set_rules('postal_address', 'Postal Address', 'trim|required');
        $this->form_validation->set_rules('contact_name', 'Contact Name', 'trim|required|min_length[6]');
        $this->form_validation->set_rules('contact_number', 'Contact Number', 'trim|required|min_length[6]');
        $this->form_validation->set_rules('position', 'Position', 'trim|required|min_length[6]');
        $this->form_validation->set_rules( 'email', 'Email', 'trim|required|valid_email|min_length[7]');


          $name_array = array();
          $count = count($_FILES['userfile']['size']);

          foreach($_FILES as $key=>$value)

          for($s=0; $s<=$count-1; $s++)

{

                  $_FILES['userfile']['name']=$value['name'][$s];
                  $_FILES['userfile']['type']    = $value['type'][$s];
                  $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
                  $_FILES['userfile']['error']       = $value['error'][$s];
                  $_FILES['userfile']['size']    = $value['size'][$s];

                  $config = [

                    'upload_path'             => './customer_documents/'.$directoryname,
                    'allowed_types'         => 'gif|jpg|png|pdf',
                    'max_size'              => '1000000',
                    'overwrite'               => FALSE,
                    'remove_spaces'         => TRUE,
                    'encrypt_name'       => FALSE
              ];
                  $this->upload->initialize($config);
                  $this->upload->do_upload();
                  $file_data = $this->upload->data();
                  $name_array[] = $file_data['file_name'];
 }




        if($this->form_validation->run() == FALSE && !$this->upload->do_upload()) 

{


            $data['error'] = $this->upload->display_errors();
            $data['services'] = $this->customer->get_service_list();
            //Load Template
            $data['page_class'] = 'register-page';
            $data['title'] = 'Registration';
            $data['content_view'] = 'Home/register_v';
            $this->template->login_template($data);
          }

          else

         {

           $names= implode(',', $name_array);

    $data  = array(
      'name_of_org'             => $this->input->post('name_of_org'),
      'address'                   => $this->input->post('address'),
      'tel'                       => $this->input->post('tel'),
      'postal_address'          => $this->input->post('postal_address'),
      'contact_name'              => $this->input->post('contact_name'),
      'contact_number'          => $this->input->post('contact_number'),
      'role'                      => 'admin',
      'position'                  => $this->input->post('position'),
      'email'                       => $this->input->post('email'),
      'userfile'                  => $names,
      'service'                   => $this->input->post('service_id'),


    );
    //Insert User
    $this->user->register($data);


        //isset Message
        $this->session->set_flashdata('success', 'You have successfully submitted your registration.');

        //Redirect
        redirect('Home/register');

  }
}

型号:

class User_m extends CI_MODEL{
 function __construct()
 {

  parent::__construct();
  $this->table = 'users';

}



function register($data)
{
  $this->db->insert($this->table, $data);
 }
}

当我使用上面的代码通过 mvc 上传文件时,它工作得很好,但是当我将它传输到 hmvc 时,它只是将其他表单字段发布到数据库中而不包含文件,同时还创建目录而不上传任何文件。可能是什么问题?任何对此有解决方案的人都被卡住了。请帮忙!

【问题讨论】:

  • 也许不是解决方案,但以防万一尝试if (!is_dir('./customer_documents/'.$directoryname))。注意./开头。
  • @Tpojka 谢谢,但在我对代码进行了一些更改后,现在我设法插入数据库,但没有文件上传到文件夹中。
  • 尝试使用绝对路径 (!is_dir(FCPATH.'customer_documents/'.$directoryname)) 并在所有地方相应地修复它。
  • 同样CI_MODEL 应该是CI_Model
  • 感谢@Tpojka 它有效

标签: php codeigniter


【解决方案1】:

用绝对路径试试

(!is_dir(FCPATH.'customer_documents/'.$directoryname)) 

并在所有地方相应地修复它。

CI_MODEL 也应该是 CI_Model

【讨论】:

    猜你喜欢
    • 2014-12-23
    • 2016-09-13
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    相关资源
    最近更新 更多