【问题标题】:Codeigniter Multi Save Upload PathCodeigniter 多保存上传路径
【发布时间】:2021-09-07 13:47:04
【问题描述】:

我尝试将上传的文件保存在 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 键两次,它只是覆盖了自己。您需要完全处理两次上传,两个目录不同。
  • 谢谢,兄弟你知道密码吗?
  • 问题不是很清楚,但是为什么不把文件上传到第二个位置后直接复制呢? SO上有很多例子,例如stackoverflow.com/questions/5772769/…

标签: php codeigniter


【解决方案1】:

您设置了 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');

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 2012-01-22
    • 2017-02-09
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多