首页 > 解决方案 > 图像不保存到临时文件夹只保存到数据库

问题描述

我希望上传保存到我的项目目录中的 upload/images 文件夹,但事实并非如此。上传保存到数据库目录时,upload/images 文件夹为空

public function addArticleForm(){
//check whether user upload picture

if(!empty($_FILES['image_path']['upload_path'])){
    $config['upload_path'] = 'uploads/images/';
    $config['allowed_types'] = 'jpg|jpeg|png|gif|';
    $config['file_name'] = $_FILES['image_path']['upload_path'];

    //load upload library and initialize configuration
    $this->load->library('upload',$config);
    $this->upload->initialize($config);

    if($this->upload->do_upload('image_path')){
        $uploadData = $this->upload->data();
        $image_path = $uploadData['file_name'];
        }else{
        $image_path = '';
        }

}

标签: phpcodeigniter

解决方案


试试这个为图片上传设置动态功能

public function upload_images($path,$filename){
    $config = array(
        'upload_path' => $path,
        'allowed_types' => "gif|jpg|png|jpeg",
        'overwrite' => TRUE,
        'max_size' => "204800", // 20 mb 
    );

    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if($this->upload->do_upload($filename)){
      $upload_data = $this->upload->data();
        $datas = array('file_name' => $upload_data['file_name'], 'file_size'=>$upload_data['file_size'], 'file_type'=>$upload_data['file_type']);
        $file_name = $upload_data['file_name'];
        $file_size = $upload_data['file_size'];
        $file_type = $upload_data['file_type'];
    } else {
        $datas = array('error' => $this->upload->display_errors());
        $this->session->set_flashdata('error_msg',$datas['error']);
    }
    return $datas;
}

并在另一个函数中使用上述函数,比如

public function Add_Customer_Profiles(){
    if($this->session->userdata('userid')!="" || null!==$this->session->userdata('userid')){
        $custno = $this->input->post('custno',TRUE);
        $fname = $this->input->post('fname',TRUE);
        $lname = $this->input->post('lname',TRUE);
        $email = $this->input->post('email',TRUE);
        $mobile = $this->input->post('mobile',TRUE);
        $path = './assets/profile_data/';
        $img_name = 'customer_img'; // Your file input name
        $file_detail = $this->upload_images($path,$img_name);
        
        if(!isset($file_detail['error'])){ 
          //Your Code here 
        }
}
        

推荐阅读