提交带有上传图片选项的表单时出错。 PHP 代码点火器
Posted
技术标签:
【中文标题】提交带有上传图片选项的表单时出错。 PHP 代码点火器【英文标题】:Error submitting a form with upload pic option. PHP Codeigniter 【发布时间】:2017-05-23 19:27:23 【问题描述】:我一直在尝试将图片上传到文件夹并将其路径存储在数据库中,我的代码似乎可以正常工作,但不是,单击提交按钮后,它会进入空白页面。 当看到发送的参数时,在我的浏览器中使用检查元素,网络选项 我从文本字段中看到正确的输入,但对于图像, 内容处置:表单数据;名称=“我的形象”;文件名="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));
型号:
public function save_new_posts($dataarray)
$this->db->insert('blogs', $dataarray);
if($this->db->affected_rows()>0)
return true;
【问题讨论】:
您的<form>
标签上有enctype="formdata/multipart"
吗?当我忘记一次时,我遇到了类似的问题
是的,我已经有了
【参考方案1】:
我的代码始终有效,它与您的方法有些不同,希望对您有所帮助。
$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
【讨论】:
以上是关于提交带有上传图片选项的表单时出错。 PHP 代码点火器的主要内容,如果未能解决你的问题,请参考以下文章