• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

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

0
czechmate1976
Asked:
czechmate1976
2 Solutions
 
hieloCommented:
if( count($_FILES) )
{
  print_r( $_FILES);
}
else
{
  echo "empty";
}
0
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now