简体   繁体   中英

Image not resize and upload in Codeigniter PHP

I am trying to create thumbnail(resize) of uploaded image using codeigniter, But image is not uploaded in server/folder,Here is my code, Where I went wrong ?

if (isset($_FILES['image'])) {
    if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {

        $filename         = time() . uniqid(rand()) . $_FILES['image']['name'];
        move_uploaded_file($_FILES["image"]["tmp_name"], "vendorProfile/" . $filename);
        $saveArr['image'] = $filename;
        $this->load->library('image_lib');

        $source_paths = base_url() . 'vendorProfile/' . $filename;
        $source_path  = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/' . $filename;
        $target_path  = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/thumb';

        $config_manip = array(
            'image_library'  => 'gd2',
            'source_image'   => $source_path,
            'new_image'      => $target_path,
            'maintain_ratio' => TRUE,
            'create_thumb'   => TRUE,
            'thumb_marker'   => '_thumb',
            'width'          => 150,
            'height'         => 150
        );

        $this->load->library('image_lib', $config_manip);
        if (!$this->image_lib->resize()) {
            echo $this->image_lib->display_errors();
        } else {
            echo "image is uploaded";
        }
        $this->image_lib->clear();
    } 
}       

You just look at the file path which you pass on $config_manip array where you need to pass file path without Http or https PHP don't accept Http. So if in your case your file struct like something this

-
application
system
image_folder <----

Then you just pass like something this

$config_manip = array(
        'image_library'  => 'gd2',
        'source_image'   => './image_folder/'.$file_name,
        'new_image'      => './thumb_folder/'.$target_path,
        'maintain_ratio' => TRUE,
        'create_thumb'   => TRUE,
        'thumb_marker'   => '_thumb',
        'width'          => 150,
        'height'         => 150
    );

If you are using the ubuntu server you need to give permission for that folder to upload the file/image.

if (!file_exists($target_path)) {
    if (!mkdir($target_path, 0777, true)) {
        chmod($target_path, 0777);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM