【问题标题】:Trying to upload multiple resized images in codeigniter php尝试在codeigniter php中上传多个调整大小的图像
【发布时间】:2020-07-06 20:17:59
【问题描述】:

我正在尝试将多个图像上传到一个文件夹并在数据库中保存一个加密的图像名称。但是,我编写的代码调整了图像的大小,但将原始图像名称保存在数据库中。

例子:如果加密后的原始文件是abc.jpg,那么调整大小的就是xyz.jpg。 db 保存 abc.jpg。

for($i=0; $i<$cpt; $i++)
    {
        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile']['size'][$i];

        //new code
        $config['upload_path'] = './uploads/advert_images';
        $target_path = './uploads/advert_images/thumbs';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1000000'; //limit 1 mb
        $config['remove_spaces'] = true;
        $config['overwrite'] = false;
        $config['encrypt_name'] = TRUE;
        $config['max_width'] = '5000';// image max width
        $config['max_height'] = '5000';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $this->upload->do_upload('userfile');
        $fileName = $_FILES['userfile']['name'];
        $data = array('upload_data' => $this->upload->data());
        if(!$this->upload->do_upload('userfile'))
        {
            $error = array('upload_error' => $this->upload->display_errors());
            $this->session->set_flashdata('error',  $error['upload_error']);
            echo $files['userfile']['name'][$i].' '.$error['upload_error']; exit;

        } // resize code
        $path=$data['upload_data']['full_path'];
        $q['name']=$data['upload_data']['file_name'];
        $configi['image_library'] = 'gd2';
        $configi['source_image']   = $path;
        $configi['new_image']   = $target_path;
        $configi['maintain_ratio'] = FALSE;
        $configi['width']  = 300; // new size
        $configi['height'] = 300;
        $this->load->library('image_lib');
        $this->image_lib->initialize($configi);
        $this->image_lib->resize();
        unlink($path);

        $insert[$i]['picture_file_name']=$this->upload->data('file_name');;

        $insert[$i]['add_id']=$last_insert_id;
    }

    $response=$this->db->insert_batch('product_pictures',$insert);

【问题讨论】:

  • 所以你想在数据库中存储调整大小的图像名称?为什么不将调整大小的图像名称设置为 abc-resized.jpgresized/abc.jpg 而不是 xyz.jpg 并从存储在数据库中的原始图像名称中获取调整大小的图像名称

标签: php codeigniter


【解决方案1】:

在您的File Upload Preferences 中,除了encrypt_name(),您还需要设置file_name()

如果设置 CodeIgniter 会将上传的文件重命名为此名称。这 文件名中提供的扩展名也必须是允许的文件类型。 如果未在原始文件名中提供扩展名,则将使用。

【讨论】:

  • 这很好用,还有如何删除位置 $config['upload_path'] = './uploads/advert_images'; 中的文件调整大小完成后,我不想保留文件。我已将其设为“unlink($path);”但文件仍然存在
  • @dinesh 如何删除文件:我建议你提出一个新问题,单独处理这个问题。
  • 我发布了一个新问题stackoverflow.com/questions/62774239/…
猜你喜欢
  • 2020-04-12
  • 2015-06-19
  • 2013-12-07
  • 1970-01-01
  • 2014-03-14
  • 2013-09-19
  • 1970-01-01
  • 2011-01-10
  • 1970-01-01
相关资源
最近更新 更多