Welcome Guest, Not a member yet? Register   Sign In
How to Check if a file has been selected for upload?
#1

[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'))
#2

[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.
#3

[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
#4

[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'].

UPLOAD_ERR_OK

    Value: 0; There is no error, the file uploaded with success.
UPLOAD_ERR_INI_SIZE

    Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
UPLOAD_ERR_FORM_SIZE

    Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
UPLOAD_ERR_PARTIAL

    Value: 3; The uploaded file was only partially uploaded.
UPLOAD_ERR_NO_FILE

    Value: 4; No file was uploaded.
UPLOAD_ERR_NO_TMP_DIR

    Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
UPLOAD_ERR_CANT_WRITE

    Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
UPLOAD_ERR_EXTENSION

    Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.
#5

[eluser]JamesTaylor[/eluser]
cheers Stef




Theme © iAndrew 2016 - Forum software by © MyBB