提交带有上传图片选项的表单时出错。 PHP Codeigniter

时间:2017-05-23 19:27:23

标签: php codeigniter

我一直在尝试将图片上传到文件夹并将其路径存储在数据库中,我的代码似乎正常工作,但不是没有,点击提交按钮后,它会转到空白页面。 在查看发送的参数时,在我的浏览器中使用Inspect元素,Network选项 我看到来自文本字段的正确输入但是对于图像, 内容 - 处置:表单数据; NAME = “MYIMAGE”;文件名= “IMG_8971.JPG” 内容类型:image / jpeg 还有其他一些看起来很奇怪的字符和符号,如:

  

ÿØÿà

控制器:

private function setup_upload_option()
{
    $config = array();
    $config['upload_path'] = 'blog/uploads/';
    $config['allowed_type']='jpg|jpeg|png|gif';
    $config['encrypt_name']= TRUE;
    $config['overwrite']=FALSE;

    return $config;
}
public function post_new_blog()
{
    $this->form_validation->set_rules('title', 'Title of the Blog', 'trim|required');
    $this->form_validation->set_rules('desc', 'Content of the Blog', 'trim|required');
    $this->form_validation->set_rules('tags', 'Tags fo the blog', 'trim|required');

    if($this->form_validation->run()==FALSE) {
        $this->session->set_flashdata('fail', validation_errors());
        $this->load->view('blogsection/addblog', array('logged_in' => $this->logged_in));
    }
    else {
        $this->load->library('upload');

        $files = $_FILES;
        $count = count($_FILES['myimage']['name']);


        for($i=0; $i<$count ;$i++)
        {
            $_FILES['myimage']['name'] = $files['myimage']['name'][$i];
            $_FILES['myimage']['type'] = $files['myimage']['type'][$i];
            $_FILES['myimage']['size'] = $files['myimage']['size'][$i];
            $_FILES['myimage']['tmp_name'] = $files['myimage']['tmp_name'][$i];

            $this->upload->initialize($this->setup_upload_option());

            if($this->upload->do_upload()==TRUE)
            {
                $data = $this->upload->data();
                $config1['image_library'] = 'gd2';
                $config1['source_image']  = $data['full_path'];
                $config1['new_image']     = 'blog/uploads/thumbs/';
                $config1['create_thumb']  = false;
                $config1['height']        = 200;
                $config['width']          = 200;

                $this->load->library('image_lib', $config1);
                $this->image_lib->initialize($config1);
                $this->image_lib->resize();

                $mydata = $this->session->all_userdata();
                $dataarray = array(
                    'blog_title' => $this->input->post('title', true),
                    'blog_content' => $this->input->post('desc', true),
                    'blog_tags' => $this->input->post('tags', true),
                    'blog_image_name' => $data['orig_name'],
                    'blog_image' => $data['full_path'],
                    'date_posted' => date(" jS \of F Y "),
                    'posted_by' => $mydata['username']
                );

                $this->main_model->save_new_posts($dataarray);

                $this->load->view('blogsection/addblog', array('logged_in' => $this->logged_in, 'success' => 'Blog was posted successfully',$dataarray));


            }
        }

    }

}

MODEL:

public function save_new_posts($dataarray)
{
    $this->db->insert('blogs', $dataarray);
    if($this->db->affected_rows()>0)
    {
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

我的代码始终有效,它与您的方法有所不同,我希望它能为您提供帮助。

$imgpath='uploads/profile_pic/';
            $all_img="";
            if($_FILES["profile_pic"]['name'])//form input type file
            {         
               $path_parts = pathinfo($_FILES["profile_pic"]["name"]);
               $image_path = $path_parts['filename'].'_'.time().'.'.$path_parts['extension']; 
               $all_img.=$image_path;
               move_uploaded_file($file_tmp=$_FILES["profile_pic"]["tmp_name"],$imgpath."/".$image_path);
               $data['cm_user_profile_pic']=$all_img;//store image name in array
            }