Codeigniter权限被拒绝图片上传

时间:2016-01-28 08:25:42

标签: php codeigniter

我正在学习如何使用codeigniter上传图像,并且当我点击没有任何图像的上传时我得到了错误权限被拒绝,如果我选择图像,它会很好用。我只是希望它会显示我的信息:请上传图片,你能看看我的代码并帮我解决我的问题。 错误说: enter image description here 这是我的观看代码:

<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>

这是我的控制器代码:

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);

}
}

这是我的模特

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;
}
}
你可以帮忙吗?

4 个答案:

答案 0 :(得分:1)

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

设置目标图像名称/路径。创建图像副本时,您将使用此首选项。 路径必须是相对或绝对服务器路径,而不是URL。

将new_image设置为文件夹,而不是文件名。使它像$ this-&gt; gallery_path。 '/thumbs/my_new_file.ext'

答案 1 :(得分:0)

您是否可以访问终端?转到项目文件夹的根目录,在终端sudo chmod -R 755 *sudo chmod -R 777 *中键入以下内容 第一种选择在某些项目中存在问题,因此是第二种选择。

答案 2 :(得分:0)

不检查错误,不要直接上传图像。在上传之前检查文件中是否有任何错误。更改您的功能如下:

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;
}

有关详细信息,请参阅HERE

答案 3 :(得分:0)

首先验证图像上传与否,然后调整图像大小

$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;