在两个不同的路径中上传图像 Codeigniter

Upload Images in two different paths Codeigniter

我会尝试将图片上传到两个不同的路径,但它总是只上传到一个路径,这是我在模型中的代码:

public function save()
{
    $post = $this->input->post();
    $this->product_id = uniqid();
    $this->name = $post["name"];
    $this->price_awal = $post["price_awal"];
    $this->price = $post["price"];
    $this->image = $this->_uploadImage();
    $this->image2 = $this->_uploadImage2('./upload/');
    $this->jenis_produk = $post["jenis_produk"];
    $this->warna = $post["warna"];
    $this->description = $post["description"];
    $this->db->insert($this->_table, $this);
}

上传功能(模型中也有)

private function _uploadImage()
{
    $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
    // $config['max_width']            = 1024;
    // $config['max_height']           = 768;

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

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

private function _uploadImage2($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";
}

只有_uploadImage函数有效,_uploadImage2无效

你能告诉我哪里出了问题吗?

问题是你已经初始化了上传库,所以要重新初始化。

此外,你应该尝试嵌套函数,这样它们就会被链式调用,或者你不想只在第二个函数中使用 $this->upload->initialize($config);:

private function _uploadImage($path)
{
    $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
    // $config['max_width']            = 1024;
    // $config['max_height']           = 768;

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

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

private function _uploadImage2($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->upload->initialize($config);

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

return "default.jpg";
}