codeigniter无法将图片上传到文件夹

时间:2015-04-11 12:40:54

标签: php image codeigniter upload

我需要帮助才能将图片上传到文件夹中。这就是事情,我之前已经测试过并且有效。过了一会儿,我再次测试它并且它没有工作。我真的无法直接思考,请帮忙!

我的观点:

<?php 
        echo form_open_multipart("adminFolder/admin/insert_picture");

        echo form_upload("userfile", "Gambar Picture");

        echo form_submit("input_picture", "Input now !!!");

        ?>

我的控制员:

public function insert_picture(){
        $this->model_get->doUpload();
    }

我的模特:

function doUpload(){
        $path = './assets/images/';
        chmod($path, 0777);

        $config['upload_path'] = $path; 
        $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
        $config['max_size'] = '6000'; 
        $config['max_width']  = '1024';
        $config['max_height']  = '768'; 
        $config['overwrite'] = TRUE;

        $this->load->library("upload", $config);

        if(!$this->upload->do_upload()){
            redirect("adminFolder/admin/insertPicture");
        }else{

            redirect("adminFolder/admin/adminPic");
        }


    }

2 个答案:

答案 0 :(得分:3)

你应该要求在do_upload中传递文件名:

所以你的模型代码将是:

  if(!$this->upload->do_upload('userfile')){
        redirect("adminFolder/admin/insertPicture");
    }else{

        redirect("adminFolder/admin/adminPic");
    }

这将有效!!请尝试

答案 1 :(得分:1)

在视图中使用:

input type =“file”multiple =“multiple”id =“userfile”name =“userfile”

<强>控制器
在您的控制器方法中添加此行
 $这 - &GT; gallery_model-&GT; do_upload($数据);

创建一个模型类“gallery_model”

var $gallery_path;
var $gallery_path_url;

function Gallery_model() {
    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 $file) {
        $images []= array (
            'url' => $this->gallery_path_url . $file,
            'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
        );
    }

    return $images;
}

如果您需要多张图片上传,我也可以粘贴代码。!!

相关问题