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
$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);

}

Open in new window

czechmate1976Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
if( count($_FILES) )
{
  print_r( $_FILES);
}
else
{
  echo "empty";
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
czechmate1976Author Commented:
returns array with keys : where the error key indicates 4
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??
Array ( [image_1] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) [image_2] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) [image_3] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) [image_4] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) [image_5] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )

Open in new window

0
czechmate1976Author Commented:
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
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

hieloCommented:

foreach ($images as $fieldname => $subArr )
{
 	if($subArr['size'] > 0)
	{
	  echo "processing $fieldname<br>";
	}
	else
	{
	 echo "$fieldname is empty<br>";
	}
}

Open in new window

0
hieloCommented:
BTW, on this:
 if (!empty($value))

value is the "subArray", which is NOT empty. You need to inspect any of the properties of the sub Array.
0
czechmate1976Author Commented:
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
0
hieloCommented:
Like I said,

"...inspect ANY of the properties of the sub Array".

So, yes:
if( !empty($value['name']) )

is fine.
0
Avinash ZalaWeb ExpertCommented:
check this:


<?php

  if(isset($_FILES['name']) && !empty($_FILES['name']))
{
   // files uploaded
}
else
{

   // no files uploaded
}

?>

Hope this helps
Addy
0
Mehul_PanchalCommented:
<?php

  if(isset($_FILES['name']) && !empty($_FILES['name']))
{
   // files uploaded
}
else
{

   // no files uploaded
}

?>
0
czechmate1976Author Commented:
thanks mate a lot, you really helped to resolve my problem and think about things slightly differently :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.