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
File uploading in CodeIgniter has two primary parts—the frontend and the backend. The frontend is taken care of by the HTML form that uses the form input type file. On the backend, the file upload library forms the submitted input from the form and writes it to the transfer registry.
Upload any File – Codeigniter$this->upload->set_allowed_types('*'); Call the upload library first by using this->load->library('upload'). set allowed type to * will make any file upload in codeigniter.
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';
form tag should contain enctype="multipart/form-data"
.
ie,
<form action='' method=''enctype="multipart/form-data>
<input type='file' name='field_name'/>
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.
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()
make sure you use
form_open_multipart()
if you are using the form helper.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With