I am trying to successfully use the PHP empty()
function. Currently my script references the input file field variable:
<input class="fileUpload" name="flyer" type="file" />
like this:
if (empty($_FILES['flyer']['name'])) {
//don't update the file
}
else {
//update the file url
}
This currently updates the file URL to a value of null
if it's empty. The problem could be something else but I think it's my variable reference. How can I correct this to get the empty
function to work?
if it is a FILE type then you need to use the $_FILES[-name-]['error'] handler to check the quality of file, check http://php.net/manual/en/features.file-upload.errors.php
Ignore the name value, what you want is
if ($_FILES['flyer']['error'] == 4) {
//// this file given was empty.
}
Apart from $_FILES['flyer']['error'] you can also check whether any file exists or not at the desired location.
if(!file_exists($_FILES['flyer']['tmp_name']) || !is_uploaded_file($_FILES['flyer']['tmp_name'])) {
echo 'No upload';
}
Check this link for detailed information about is_uploaded_file()