开发者

Unsetting empty array elements

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 b开发者_如何学Pythonack with empty elements. Is there actually a sane way of deleting empty $_FILES array elements with PHP?


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


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)


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


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


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

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜