且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

图片上传在Codeigniter中不起作用

更新时间:2023-02-06 11:57:06

在控制器中编写以下函数.

Write the below Function in your Controller.

/*It will upload the images to the specified path and will return the image urls in an array*/

private function upload_files($path, $files)
{
    $path = PHYSICAL_PATH . $path;
    $config = array(
        'upload_path'   => $path,
        'allowed_types' => 'JPG|JPEG|GIF|PNG|gif|jpg|png|jpeg|tft|TFT',
        'overwrite'     => 1,                       
    );

    $this->load->library('upload', $config);
    $images = array();      
    foreach ($files['name'] as $key => $image) {
        $_FILES['images[]']['name']= $files['name'][$key];
        $_FILES['images[]']['type']= $files['type'][$key];
        $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
        $_FILES['images[]']['error']= $files['error'][$key];
        $_FILES['images[]']['size']= $files['size'][$key];

        $fileName = time() .'_'. $image;

        $images[] = $fileName;

        $config['file_name'] = $fileName;

        $this->upload->initialize($config);

        if ($this->upload->do_upload('images[]')) {
            $this->upload->data();
        } else {
            echo $this->upload->display_errors();die();
            return false;
        }
    }
    return $images;
}

在您的函数中调用上述函数

call the above Function in your function

$firstFileName = $_FILES['photo']['name'][0];//Check whether any Images is uploaded or not.
if($firstFileName != "")
{   
    $path = "uploads/property_image/";//Give your Path name
    $values = $this->upload_files($path,$_FILES['photo']);
/* $values contents the Image path Array.Now you can send the array to the View Page for your use or can call any Models to store into the DB. */

}