I'm sure this is sim开发者_如何学Gople but I can't see how to make uploading a file with CI optional.
If you leave the file input box empty, the error "You didn't choose an upload file" appears.
The reason I want it to be optional is that my form edits a directory type listing, and I don't need to upload the image each time I edit the listing.
Is there a way to remove the "required" error handling on the file class
Use the following:
<?php if ( $_FILES AND $_FILES['field_name']['name'] )
{
// Upload the file
}
codeigniter file upload optionally ...works perfect..... :)
---------- controller ---------
function file()
{
$this->load->view('includes/template', $data);
}
function valid_file()
{
$this->form_validation->set_rules('userfile', 'File', 'trim|xss_clean');
if ($this->form_validation->run()==FALSE)
{
$this->file();
}
else
{
$config['upload_path'] = './documents/';
$config['allowed_types'] = 'gif|jpg|png|docx|doc|txt|rtf';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile',FALSE))
{
$this->form_validation->set_message('checkdoc', $data['error'] = $this->upload->display_errors());
if($_FILES['userfile']['error'] != 4)
{
return false;
}
}
else
{
return true;
}
}
i just use this lines which makes it optionally,
if($_FILES['userfile']['error'] != 4)
{
return false;
}
$_FILES['userfile']['error'] != 4 is for file required to upload.
you can u make it unneccessory by using $_FILES['userfile']['error'] != 4
, then it will pass this error for file required and
works great with other types of errors if any by using return false ,
hope it works for u ....
Use this code in the controller before calling do_upload()
if (is_uploaded_file($_FILES['field_name']['tmp_name'])) {
// your code here
}
Use This Code :-
$config['upload_path'] = 'assets/img/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
// Upload the file
if ($this->upload->do_upload('Image')){
$dataimage = $this->upload->data();
$data = array(
'image' => $dataimage['file_name'],
'UserName' => $this->input->post('UserName'),
'Password' => $this->input->post('Password'),
'xid' => $this->input->post('xid')
);
}
else{
/*$out['msg'] = show_err_msg($this->upload->display_errors());
echo json_encode($out);
exit();*/
$data = array(
'image' => NULL,
'UserName' => $this->input->post('UserName'),
'Password' => $this->input->post('Password'),
'xid' => $this->input->post('xid')
);
}
精彩评论