How to Check if a file has been selected for upload? |
[eluser]JamesTaylor[/eluser]
I'm trying to get a form working which allows the option to upload a file... However, choosing to upload a file is not a requirement of the form, so i how can check if the user has choosen a file for upload? As i need to execute some extra code if an upload if going to be needed. I think i read somewhere that we can't check the post data for the file upload input as it doesn't exist in the same way that the usual data from input boxes does. i.e - I can't go Code: if ($this->input->post('userfile'))
[eluser]stef25[/eluser]
Uploaded file data is not available in the $_POST. If you do a print_r($_FILES) after a form has been submitted with NO upload, you'll see that $_FILES['error'] = 4 (I think, something along those lines at least). So to check if a file has been uploaded you do something like if($_FILES['error'] !== 4) then file was uploaded. If the value is 4 then proceed as if no file was uploaded.
[eluser]JamesTaylor[/eluser]
Thanks Stef, thats working but i actually needed to use the follwoing to access the error Code: $_FILES['userfile']['error'] !==4 Would i be right in thinking that different numbers will related to different errors - if so do you know where i can find a reference of the error numbers? Many Thanks
[eluser]stef25[/eluser]
Via Google "file upload error codes": http://php.net/manual/en/features.file-u...errors.php There are 8 different error codes. Code: Since PHP 4.2.0, PHP returns an appropriate error code along with the file array. The error code can be found in the error segment of the file array that is created during the file upload by PHP. In other words, the error might be found in $_FILES['userfile']['error'].
|
Welcome Guest, Not a member yet? Register Sign In |