【问题标题】:codeigniter can't upload picture to foldercodeigniter无法将图片上传到文件夹
【发布时间】:2015-06-17 03:34:10
【问题描述】:

我需要帮助上传文件夹中的图片。事情是这样的,我之前测试过,它有效。过了一会儿,我再次测试它并繁荣它不起作用。我实在想不通,请帮忙!

我的看法:

<?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");
        }


    }

【问题讨论】:

    标签: php image codeigniter upload


    【解决方案1】:

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

    所以你的模型代码将是:

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

    这将起作用!请尝试

    【讨论】:

    • 我试过了,还是一样,有趣的是我将确切的代码复制到另一个空项目中,它可以工作,但在我的主要项目上不起​​作用,不知道该说什么了跨度>
    • 请写 echo $this->upload->display_errors();出口;在 if 代码和重定向行之前,它会在上传时回显错误
    【解决方案2】:

    在视图中使用:

    input type="file" multiple="multiple" id="userfile" name="userfile"

    控制器
    在您的控制器方法中添加此行
    $this->gallery_model->do_upload($data);

    创建一个模型类“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;
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-27
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多