3

I'm working with $_FILES and sometimes the array has empty array elements due to empty file inputs on my form. I'm trying to unset these elements.

I've tried these code snippets:

foreach($_FILES['images']['name'] as $image)
{
    if(empty($image))
    {
        unset($image);
    }
}

foreach($_FILES['images']['name'] as $image)
{
    if($image == "")
    {
        unset($image);
    }
}

foreach($_FILES['images']['name'] as $image)
{
    if(!$image)
    {
        unset($image);
    }
}

But the array always comes back with empty elements. Is there actually a sane way of deleting empty $_FILES array elements with PHP?

John
  • 187
  • 1
  • 7

5 Answers5

5

When you use foreach($_FILES['images']['name'] as $image) statement $image becomes a copy of the actual element in the array, what you are doing is unsetting that copy, this is how you should do it:

foreach( $_FILES['images']['name'] as $key => $value ) {
    if( empty($value) ) {
        unset( $_FILES['images']['name'][$key] );
    }
}
nobody
  • 10,599
  • 4
  • 26
  • 43
1

How about this non-loopy answer?

$in = $_FILES['images']['name'];
$out = array_filter($in);

Or if you prefer one line:

$out = array_filter($_FILES['images']['name']);

From the manual page for array_filter:

"If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed."

JohnK
  • 6,865
  • 8
  • 49
  • 75
1

to start with, your question is not specific because if u are working with asingle file there is no need of foreach( ($_FILES['images']['name'] as $image). again u metioned empty fields in your form, this ought to trigger case 4 error. That is no file was uploaded. so with ur error method set like this

if($_FILES['upload']['error'] > 0){
echo 'the file couldnt be uploaded because';
 switch($_FILES['upload']['error']){
  case 1:
 print 'the file exceeds max size in php.ini';
 break;
 case 2:
  print 'the file exceeds max size in html settings';
 break;
  case 3:
 print 'the file was partially uploaded';
 break;
 case 4:
 print 'no file was uploaded';
 break;
 case 6:
 print 'no temporary folder available';
 break;
 case 7:
  print 'unable to write to disk';
 break;
 case 8:
print 'file upload stopped';
 break;
default:
print 'a sys error occured';
break;

With this an error is notified and u know that an empty image as been uploaded. to save urself the stress of UNSET(). if it is multi uploads you will have something like

foreach ($_FILES['upload']['name'] as $number => $filename)
bolaji
  • 129
  • 1
  • 6
0

In addition to nobody's answer, if you also want to strip the element from the type, tmp_name, size etc. arrays use:

// Before stripping
print_r($_FILES);

$length = count($_FILES['images']['name']);
for($i = 0; $i < $length; $i++){
    if(empty($_FILES['images']['name'][$i]))
        foreach($_FILES['images'] as $key => $value)
        unset($_FILES['images'][$key][$i]);
}

// After stripping
print_r($_FILES);
Paul
  • 139,544
  • 27
  • 275
  • 264
  • Sorry, I should have been more specific. `$_FILES['images']['name']` is an array, as shown by this print_r: http://pastebin.com/JYmCvYBU – John Aug 23 '11 at 22:36
0

using error code would be best

foreach( $_FILES['images']['error'] as $key => $value ) {

    if($value==0) { 
      // file good do code
    } else { 
    unset( $_FILES['images']['name'][$key] );
    }

    }
amigura
  • 139
  • 5