Reputation: 137
Since someone's linked their own question, with an answer that does not even remotely come close to my question, I want to:
As it stands now, even if my image is correct, but form validation returns false, my image is uploaded. I want to prevent this.
Currently I'm attempting to create a form that will allow a user to select a file to upload. I've got rules set for other inputs, as well as a callback for my image upload.
The problem is that no matter what, as long as the image passes validation, its uploaded, even if there's an error in in one of the other inputs.
Any answer that I've found on StackOverflow have been answers that put me into the same position.
$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
$this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
$this->form_validation->set_rules('approved', 'Approved', 'trim|xss_clean');
$this->form_validation->set_rules('upload', 'File Upload', 'callback_do_upload');
if(($this->form_validation->run() == FALSE))
{
view('someview', $this->data);
}
I've written my own do_upload function for my controller, which validates weather or not an user has selected an image, and if true, validates and uploads.
public function do_upload ()
{
$config['upload_path'] = './uploads/articles';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$this->load->library('upload', $config);
if(is_uploaded_file($_FILES['upload']['tmp_name']))
{
if($this->upload->do_upload('upload'))
{
$upload_data = $this->upload->data();
return TRUE;
}
else
{
$this->form_validation->set_message('do_upload', $this->upload->display_errors());
return FALSE;
}
}
else
{
return TRUE;
}
}
Say I don't enter a description into my input, but I select an image to upload, it will then run the validation on the image, upload it, and then $this->form_validation->run()
will return false due to the lack of a description. I've tried removing the $this->upload->do_upload()
from the controllers do_upload
, relying on the $_FILES
array, however, that will not validate an upload.
Upvotes: 0
Views: 330