Link to home
Start Free TrialLog in
Avatar of czechmate1976
czechmate1976Flag for United Kingdom of Great Britain and Northern Ireland

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

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of czechmate1976

ASKER

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

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>";
	}
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
check this:


<?php

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

   // no files uploaded
}

?>

Hope this helps
Addy
Avatar of Mehul_Panchal
Mehul_Panchal

<?php

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

   // no files uploaded
}

?>
thanks mate a lot, you really helped to resolve my problem and think about things slightly differently :-)