czechmate1976
asked on
PHP - how to find out if $_FILES is empty
I am trying to check if the superglobal file is empty and despite it being empty, it still passes through the !empty check. Any idea how to sort this out. I am using a simple iteration (below) to loop through all the file fields but !empty doesn't catch an empty field.
Any tips or suggestions are much appreciated
Thanks a lot
Any tips or suggestions are much appreciated
Thanks a lot
$images = $_FILES;
$count = 0;
$process_record = array(); // array to collect records of progress
foreach ($images as $fieldname => $value ) {
$count++;
if (!empty($value)) {
print_r($value) ;
//Process image
//echo $fieldname; print_r($value);echo "<br />";
$img = new Image;
$img->attach_file($value, $user->id, $count);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry.. thanks for your contribution... I eventually figured this out that there must be a value in the array but it took me a moment
foreach ($images as $fieldname => $subArr )
{
if($subArr['size'] > 0)
{
echo "processing $fieldname<br>";
}
else
{
echo "$fieldname is empty<br>";
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have now realized that.. but isn't it enough to check the $value['name'] to find out if the file field has been used?? Thanks
Like I said,
"...inspect ANY of the properties of the sub Array".
So, yes:
if( !empty($value['name']) )
is fine.
"...inspect ANY of the properties of the sub Array".
So, yes:
if( !empty($value['name']) )
is fine.
check this:
<?php
if(isset($_FILES['name']) && !empty($_FILES['name']))
{
// files uploaded
}
else
{
// no files uploaded
}
?>
Hope this helps
Addy
<?php
if(isset($_FILES['name']) && !empty($_FILES['name']))
{
// files uploaded
}
else
{
// no files uploaded
}
?>
Hope this helps
Addy
<?php
if(isset($_FILES['name']) && !empty($_FILES['name']))
{
// files uploaded
}
else
{
// no files uploaded
}
?>
if(isset($_FILES['name']) && !empty($_FILES['name']))
{
// files uploaded
}
else
{
// no files uploaded
}
?>
ASKER
thanks mate a lot, you really helped to resolve my problem and think about things slightly differently :-)
ASKER
I need to check if a file's been attached so it doesn't loop through empty fields and report errors which may not be errors..
Would this be ok: if (!empty($value['name'])) { ... }
or it's not really a correct or a good way to do it??
Open in new window