"did not select a file to upload" when uploading using codeigniter
Solution 1
Parameter to $this->upload->do_upload()
function should be the name of the form field. (If you call it without parameters userfile
would be used). It seems in your case it should be 'image'. Instead of:
$image1=$this->input->post('image');
it should be:
$image1='image';
Solution 2
- View page
form tag should contain enctype="multipart/form-data"
.
ie,
<form action='' method=''enctype="multipart/form-data>
<input type='file' name='field_name'/>
- controller
and in controller upload code should be $this->upload->do_upload("field_name")
and jsut check whether file reaches in server side by making just print like
print_r($_FILES);
if you get null array then make sure that client side code is correct.
Solution 3
I totally agree with the advice about making sure the name of the file element in the form is either userfile or, if different, is passed to the do_upload method. However, for future reference it's also worth noting this line:
$image1=$this->input->post('image');
comes before
if (!$this->upload->do_upload($image1)) {
I found the post
method of the input
class does not return anything until after the do_upload
method is called. Also, you've already called it in the if
statement so you don't need to call it again in the else
clause. After calling do_upload
you now have access to form elements with $this->input->post
and information about the uploaded file with $this->upload->data()
Solution 4
make sure you use
form_open_multipart()
if you are using the form helper.
LiveEn
Updated on February 11, 2020Comments
-
LiveEn about 4 years
im trying to upload an image but it always give me "You did not select a file to upload."
My controller
function add() { $thedate=date('Y/n/j h:i:s'); $replace = array(":"," ","/"); $newname=str_ireplace($replace, "-", $thedate); $config['upload_path'] = './upload/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $config['file_name']=$newname; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); //$this->upload->initialize($config); $this->load->library('form_validation'); $this->form_validation->set_rules('title','title','trim|required'); $this->form_validation->set_rules('description','Description','trim|required'); $image1=$this->input->post('image'); if ($this->form_validation->run()==FALSE){ $this->addview(); return false; } if (!$this->upload->do_upload($image1)) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_error', $error); } else { $mage=$this->upload->do_upload($image1); $data =array( 'title'=>$this->input->post('title'), 'descrip'=>$this->input->post('description'), 'image' => $mage['file_name'] ); $this->load->model('member_functions'); $q=$this->member_functions->insert($data); }}
all the file requirements and the file permissions are set but i still get the that eror. can someone please tell me what am i doing wrong
-
LiveEn over 12 yearsThanks alot for that... but now the image value doesn't pass file_name. i get "Column 'image' cannot be null". Is "'image' => $mage['file_name']" the correct way to pass the file name?
-
cenanozen over 12 yearsNo i assumed that 'image' was the name of the form element. you should name the form element like:
<input type="file" name="image" />
or if you want the default value for do_upload<input type="file" name="userfile" />