Codeigniter Permission denied images upload

2019-07-18 10:22发布

问题:

i am learning how to upload an image with codeigniter, and i got an error permission denied when i click on the upload without any image, it works well if i select an image. i just hoped it will show my message it is: Please upload an Images,can you have a look on my code and help me figure down my problem. the error says: This is my view code:

<head>
<title>CI upload images</title>
<meta charset="UTF-8">
</head>
<body>
<div id="gallery">
    <?php if(isset($images) && count($images)): 
    foreach ($images as $image): ?>
    <div class="thumb">
        <a href="<?php echo $image['url']; ?>">
            <img src="<?php echo $image['thumb_url']; ?>"/>
        </a>
    </div>

    ?>
    <?php endforeach; else: ?>
        <div id="blank_gallery">Please upload an Images</div>
    <?php endif; ?>
</div>
<div id="upload">
    <?php 
    echo form_open_multipart('gallery');
    echo form_upload('userfile');
    echo form_submit('upload', 'Upload');
    echo form_close();
    ?>
</div>
</body>

and this is my controller code:

class Gallery extends CI_Controller{
function index(){

    $this->load->model('Gallery_model');
    if($this->input->post('upload')){
        $this->Gallery_model->do_upload();
    }

    $data['images'] = $this->Gallery_model->get_images();
    $this->load->view('gallery_view', $data);

}
}

and here is my model

class Gallery_model extends CI_Model{

var $gallery_path;
var $gallery_path_url;

function __construct(){
    parent::__construct();

    $this->gallery_path = realpath(APPPATH . '../images');
    $this->gallery_path_url = base_url().'images/';
}
function do_upload(){
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000
        );
    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => $this->gallery_path . '/thumbs',
        'maintain_ration' => true,
        'width' => 150,
        'height' => 100
        );
    $this->load->library('image_lib', $config);
    $this->image_lib->resize();
}
function get_images(){
    $files = scandir($this->gallery_path);
    $files = array_diff($files, array('.','..','thumbs'));

    $images = array();

    foreach ($files as $files) {
        $images []= array(
            'url'=> $this->gallery_path_url . $files,
            'thumb_url' => $this->gallery_path_url . 'thumbs/' . $files
            );
    }
    return $images;
}
}

can you please help?

回答1:

  'new_image' => $this->gallery_path . '/thumbs',

Sets the destination image name/path. You'll use this preference when creating an image copy. The path must be a relative or absolute server path, not a URL.

you set new_image as folder, not filename. make it like $this->gallery_path . '/thumbs/my_new_file.ext'



回答2:

Do u hv terminal access ? Go to the root of you project folder and type the following in your terminal sudo chmod -R 755 * or sudo chmod -R 777 * The first option has issues in some some projects hence the second option.



回答3:

Do not upload the image directly without checking the errors. Before uploading check there any errors in file. Change your function as below:

function index()
{
    $this->load->model('Gallery_model');
    if ($this->input->post('upload')) {
        $errors = $this->Gallery_model->do_upload();
        if (!$errors) {
            $data['images'] = $this->Gallery_model->get_images();
            $this->load->view('gallery_view', $data);
        } else {
            // your error displaying page.
        }
    }
}

function do_upload()
{
    $error = array();
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000
    );
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
    } else {
        $this->upload->do_upload();
        $image_data = $this->upload->data();

        $config = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $this->gallery_path . '/thumbs',
            'maintain_ration' => true,
            'width' => 150,
            'height' => 100
        );
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
    }

    return $error;
}

For more information read HERE



回答4:

First Validate the image upload or not, then resize the image

$result = array();

    if (!$this->upload->do_upload('userfile')) {

        $result['error'] = $this->upload->display_errors();


    }else {

       $image_data = $this->upload->data();

     $result['image_data']      =   $image_data;    

        $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => $this->gallery_path . '/thumbs',
        'maintain_ration' => true,
        'width' => 150,
        'height' => 100
        );

        $this->load->library('image_lib', $config);
        $this->image_lib->resize();

    }

return $result;