显示错误“上传路径似乎无效”

时间:2015-06-28 06:04:22

标签: php codeigniter

我正在制作表单上传文件图片,但上传时遇到问题。

我的控制器

public function post(){ 
        $config['file_name'] = 'pict_'.date('Y_m_d_H_i_s');
        $config['upload_path'] = './monyet';
        $config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
        $config['max_size'] = '8000';
        $config['max_width']  = '1366';
        $config['max_height']  = '1024';
        $this->load->library('upload', $config);
        if($_FILES['userfile']['name'] == "") {                                 
            $datanya = array(   'titlenews'     => $this->input->post('titlenews'),
                                'userid'        => $this->input->post('userid'),
                                'postdate'      => $this->input->post('postdate'),
                                'contentnews'   => $this->input->post('contentnews')
                            );
            $this->m_news->post($datanya);
            redirect('admin/c_news/post');
        }else{
            if (!$this->upload->do_upload()){
                echo 'Error Bos';
                echo $this->upload->display_errors();
            }else{
                $data = $this->upload->data();
                $datanya = array( 
                                    'titlenews'     => $this->input->post('titlenews'),
                                    'userid'        => $this->input->post('userid'),
                                    'postdate'      => $this->input->post('postdate'),
                                    'contentnews'   => $this->input->post('contentnews'),
                                    'pictnews'      => $data['file_name']
                                );
                $this->m_news->post($datanya);
                redirect('admin/c_news');
            }
        }
    }

我的观点

<form method="post" action="<?php echo base_url('admin/c_news/post') ?>" enctype="multipart/form-data">
                <input type="hidden" name="userid" value="<?php echo $this->session->userdata('id'); ?>" />
                <input type="hidden" name="postdate" value="<?php echo gmdate("Y-m-d H:i:s", time()+60*60*7) ?>" />

                <div class="col-md-9">
                    <h4 class="header green clearfix">
                        Judul Berita
                    </h4>
                    <input type="text" class="form-control" name="titlenews" placeholder="Content News" />
                </div>

                <div class="col-md-3">
                    <h4 class="header green clearfix">
                        Gambar Berita
                    </h4>
                    <input type="file" name="userfile" class="form-control" />
                </div>

                <div class="col-md-9">
                    <h4 class="header green clearfix">
                        Isi Berita
                    </h4>
                    <textarea class="form-control" name="contentnews"></textarea>
                </div>

                <div class="col-md-12">
                    <div class="hr hr-double dotted"></div>

                    <div class="btn-group pull-right">
                        <button class="btn btn-sm btn-danger btn-white btn-round">
                            <i class="ace-icon fa fa-floppy-o bigger-125"></i>
                            Simpan
                        </button>
                    </div>
                </div>
            </form>

当我提交文件时无法上传并显示错误“上传路径似乎无效”。我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

有几种方法可以尝试

仔细检查主目录中的monyet文件夹

$config['upload_path'] = './monyet/'; 添加额外的正斜杠'/'

$config['upload_path'] = FCPATH . 'monyet/';