使用codeigniter上传时,我的图像不会插入

时间:2015-04-27 05:50:01

标签: php codeigniter

使用codeigniter上传时未插入我的图片:

function add_newblog()
{
       $sess_id = $this->session->userdata('id');
       $result['query'] = $this->login_model->profile($sess_id);
        foreach($result['query'] as $row)
          {
           $email = $row->blogger_email; 
           $url = $row->blogger_url; 
           $author = $row->blogger_name; 
       if ($this->input->post('submit')) {

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '0';
        $config['max_width'] = '0';
        $config['max_height'] = '0';

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

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            $media = 'No Image Uploaded';

        $title = $this->input->post("title");
        $category = $this->input->post('category');
        $content = $this->input->post('content'); 
                $this->blog_model->add_newblog($sess_id,$title,$category,$content,$media,$email,$url,$author);

                $this->session->set_flashdata('valid', 'Blog Created without Image');
                redirect('content_writer/add_blog');
            }
        else {
            $data = array('upload_data' => $this->upload->data());
            $data = $this->upload->data();

            $media = $data['file_name'];
            $title = $this->input->post("title");
            $category = $this->input->post('category');
            $content = $this->input->post('content'); 

                $this->blog_model->add_newblog($sess_id,$title,$category,$content,$media,$email,$url,$author);
                $this->session->set_flashdata('valid', 'Blog Created');
                redirect('content_writer/add_blog');
            }

        }
        else{
             $this->session->set_flashdata('invalid', 'Invalid');
                redirect('content_writer/add_blog');
        }
      }
    }

其他条件总是有效。图像名称不会保存在图像路径中。

3 个答案:

答案 0 :(得分:1)

问题出在这一行

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

改为使用

$config['upload_path'] = 'uploads/<folder name>'; 

或者,如果您想直接将图像保存在uploads文件夹中,请使用

$config['upload_path'] = 'uploads/';

以及使您在veiw部分中形成的内容以form_open_multipart()打开 希望这能解决你的问题

答案 1 :(得分:0)

设置你的max_size,max_width和max_height。这将是下面的一个例子。

$config['max_size'] = '3000';
$config['max_width']  = '1500';
$config['max_height']  = '1500';

答案 2 :(得分:0)

您尚未在$ this-&gt; upload-&gt; do_upload()方法中传递图像名称。

使用你的

输入类型='文件'名称= myDoc

$这 - &GT; upload-&GT; do_upload( “myDoc”)