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

问题描述 投票:0回答:1

我在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**/
    }

图像正在上传,但它们会上传到同一文件夹。任何人都可以告诉我如何使文件保存在单独的文件夹中

php codeigniter file-upload upload image-uploading
1个回答
3
投票

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

$this->upload->initialize($config2);
© www.soinside.com 2019 - 2024. All rights reserved.