文件未存储在正确的文件夹中

时间:2017-12-28 10:28:55

标签: php codeigniter file-upload upload image-uploading

我在CodeIgniter中有一个表单,允许用户上传2个单独的文件。在后端我希望这些文件存储在不同的文件夹中。在控制器中我写了上传代码

public function upload()
    {
        /**Start uploading file**/
        $config['upload_path'] = './assets/file/.';
        $config['allowed_types'] = 'gif|jpg|png|doc|txt';
        $config['max_size'] = 1024 * 8;
        $config['encrypt_name'] = TRUE;

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

        if (!$this->upload->do_upload('file')) 
            {
              $error = array('error' => $this->upload->display_errors());
              echo $error;

            }
        else
            {
              $data = $this->upload->data();
              echo $file = $data['file_name']; //name of  file

            }

        /**End uploading file**/

        /**Start uploading img**/

        $config2['upload_path'] = './assets/img/.';
        $config2['allowed_types'] = 'gif|jpg|png|doc|txt';
        $config2['max_size'] = 1024 * 8;
        $config2['encrypt_name'] = TRUE;

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

        if (!$this->upload->do_upload('img')) 
            {
              $error1 = array('error' => $this->upload->display_errors());
              echo $error1;

            }
        else
            {
              $data1 = $this->upload->data();
              echo $img = $data1['file_name']; //name of  img

            }

        /**End uploading img**/
    }

图片上传但是上传到同一个文件夹。任何人都可以告诉我如何使文件保存在单独的文件夹中

1 个答案:

答案 0 :(得分:3)

在第二次加载时,您需要对加载的库进行初始化,因为加载方法不会初始化它:

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