codeigniter上传路径似乎无效

时间:2016-12-05 04:50:40

标签: php codeigniter

问题是当上传图像时,每次显示相同的错误"上传路径似乎无效"。尝试了这么多的解决方案,但仍然有同样的问题..请帮助。

控制器:

    public function updateprofile()
        {
            /***get current user data***/
            $this->load->model('Admin_main','userinfo');
            $data=$this->userinfo->getuserdata();
            $this->load->model('Admin_main','updateprofile');
            $result=$this->updateprofile->updatemyprofile();
            if($data->identity=='admin')
            {
             redirect('admin/myprofile'); 
            }
            else
            {
             redirect('campuser/myprofile');
            }

        }

型号:

       if($_FILES['fileToUpload']['name']!='')
        {
                 $config["upload_path"] =  './uploads/'; 
                 $config["allowed_types"] = 'jpg|jpeg|png|gif';
                 $config['max_width'] = 0;
                 $config['max_height'] = 0;
                 $config['max_size'] = 0;
                 $config['encrypt_name'] = TRUE;
                //Load upload library and initialize configuration
                 $this->load->library('upload',$config);
                 $this->upload->initialize($config);
                if($this->upload->do_upload('fileToUpload')){
                    $uploadData = $this->upload->data();
                    $picture = $uploadData['file_name'];
                    $mypic = array('profile_pic'=>$picture);
                    $this->db->set($mypic);
                    $this->db->where('id', $userid);
                    $uppic=$this->db->update('mokhayam_users');
                }
                else
                {
                    echo $this->upload->display_errors();
                }
            }

查看:

 <form class="form-horizontal" action="admin/updateprofile" method="post" enctype="multipart/form-data">
             <div class="form-group">
             <label class="col-sm-2 control-label">Profile Picture</label>
           <div class="col-sm-10">
        <label for="fileToUpload"><img src="assets/admin/dist/img/gul.jpg" id="blah1" class="profile-user-img img-responsive img-circle"/></label><input type="file" name="fileToUpload" id="fileToUpload" onchange="$('#blah1')[0].src = window.URL.createObjectURL(this.files[0])">
                            </div>
                              </div>
            <input type="hidden" name="userid" value="2"> 
                              <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
   <button type="submit" class="btn btn-success">Submit</button>
            </div>
           </div>
</form>

提前致谢..

2 个答案:

答案 0 :(得分:1)

如果您的codeigniter版本是3.x,请将库文件(system / library / upload.php)版本codeigniter 3.x更改为2.x 然后尝试

$config["upload_path"] =  APPPATH.'/uploads/'; 
......

答案 1 :(得分:0)

无需更改库中的任何内容,解决方案是在控制器中加载上传库后立即初始化配置,按照此代码

// Upload Image
        $config['upload_path'] = './uploads';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '7048';
        $config['max_width'] = '7000';
        $config['max_height'] = '7000';

        // Create folder (Uploads) if not exists
        if (!is_dir($config['upload_path'])) {
            mkdir($config['upload_path']);
        }

        // Load library Upload
        $this->load->library('upload', $config);

        // Initialize the Upload library with current $config
        $this->upload->initialize($config);

        $this->upload->do_upload();
        if ($this->upload->do_upload()) {
            $data = array('upload_data' => $this->upload->data());
            $page_image = $_FILES['userfile']['name'];
        } else {
            $errors = array('error' => $this->upload->display_errors());
            $page_image = 'noimage.jpg';
        }

        // Set message
        $this->session->set_flashdata('page_created', 'Your File(s) has been uploaded');