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?

1
  • Could you add a print of the array with the empty elements? The result of a print_r($_FILES); would do. Commented Aug 23, 2011 at 22:34

5 Answers 5

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] );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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."

Comments

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

1 Comment

Sorry, I should have been more specific. $_FILES['images']['name'] is an array, as shown by this print_r: pastebin.com/JYmCvYBU
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] );
    }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.