Welcome Guest, Not a member yet? Register   Sign In
How to check if ‘userfile’ field is not empty before uploading a file?
#1

[eluser]pulok[/eluser]
How can I check if user has selected any file to upload (before calling the $this->upload->do_upload() in my controller)? This is needed because there are other input fields along with a file upload field which is not mandatory. Basically what I want to do is:
Code:
if ( user_chose_any_file_to_upload ){
   // try to upload file
   $this->upload->do_upload();
   blah.. blah.. blah..
}
#2

[eluser]Michael Wales[/eluser]
Code:
<?php
  if (isset($_FILES)) {
    // They uploaded some sheezy
  }
?>
#3

[eluser]pulok[/eluser]
Thanks Michael.
Sorry it was posted somehow twice :-(
#4

[eluser]LuckyFella73[/eluser]
Maybe it's a bit fussy to mention:
While checking the file (like Michael
stated before) the file is allready
uploaded to the temp-directory of
your webserver. You then just decide
to move the file to your decided filestructure
or not.

Nice weekend - Fussyfella Wink
#5

[eluser]pulok[/eluser]
For some reason it is not working as expected. Take the upload example in userguide at http://ellislab.com/codeigniter/user-gui...ading.html
I slightly modified the code of upload.php as follows. It did not work! Am I missing anything?
Code:
if (isset($_FILES)) {
    // code from user guide
    if ( ! $this->upload->do_upload()){
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    } else    {
        $data = array('upload_data' => $this->upload->data());
        $this->load->view('upload_success', $data);
    }
} else {
    // custom action
    $error = array('error' => 'My Error Message: Well, user decided not to upload any file this time.');
    $this->load->view('upload_form', $error);
}
#6

[eluser]LuckyFella73[/eluser]
Would be nice if you could post the whole controller code.
Your post doesn't make clear for example if you did set the config
params.
Code:
# CI Manual:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';

And please describe what kind of error do you get or what happen
(or what exactly do not happen) after submitting the form.
#7

[eluser]pulok[/eluser]
I am trying to check, if user has really selected any file to upload (before calling the do_upload ) in my controller. Regarding the code, I referred the standard upload example from CI user guide. I only added ‘if (isset($_FILES))’ block in code to check if user selected any file to upload or not, which is not working anyway.

Other words, I like to separate ‘validation’ and ‘error check’ for upload file. HTH.
#8

[eluser]elvix[/eluser]
I'm not 100% sure testing for presence of $_FILES is enough. Seems like (in my limited testing) that any multipart form with an upload is going to generate the $_FILES array regardless, which makes sense since you need to know if there was a problem (found in $_FILES['userfile']['error']).

So, I added a test for no errors, which seems to work:

Code:
isset($_FILES) && @$_FILES['userfile']['error'] == '0'




Theme © iAndrew 2016 - Forum software by © MyBB