Codeigniter 多保存上传路径

Codeigniter Multi Save Upload Path

我尝试将上传的文件保存在 2 个路径中,一个在文件夹内并包含 id,一个在文件夹外

这是我的代码:

private function _uploadImage()
{
    $config['upload_path']          = './upload/'.$this->product_id;
    $config['allowed_types']        = 'gif|jpg|png';
    $config['file_name']            = $this->product_id;
    $config['overwrite']            = true;
    $config['max_size']             = 1024; // 1MB
    
    $config['upload_path']          = './upload/product';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['file_name']            = $this->product_id;
    $config['overwrite']            = true;
    $config['max_size']             = 1024; // 1MB
    
    if (!is_dir('upload/'.$this->product_id)) {
        mkdir('./upload/' . $this->product_id, 0777, TRUE);
    }

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

    if ($this->upload->do_upload('image')) {
        return $this->upload->data("file_name");
    }
    
    return "default.jpg";
}

当我运行那个代码的时候,结果卡住了

你能帮我看看哪里出了问题吗?

您设置了两次 upload_path 键,它只是覆盖了自己。您需要将整个上传处理两次,并且您的两个目录不同。

由于您使用的是私有函数,因此您可以添加一个参数来区分路径

private function _uploadImage($path)
{
    $config['upload_path']          = $path . '/' . $this->product_id;
    $config['allowed_types']        = 'gif|jpg|png';
    $config['file_name']            = $this->product_id;
    $config['overwrite']            = true;
    $config['max_size']             = 1024; // 1MB
    
    if (!is_dir($path . $this->product_id)) {
        mkdir($path . $this->product_id, 0777, TRUE);
    }

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

    if ($this->upload->do_upload('image')) {
        return $this->upload->data("file_name");
    }
    
    return "default.jpg";
}

现在,当您调用函数时,只需包含路径:

$this->_uploadImage('./upload');
$this->_uploadImage('./upload/product');