取消设置空数组元素

发布于 2024-11-30 23:29:01 字数 491 浏览 1 评论 0原文

我正在使用 $_FILES ,有时由于表单上的文件输入为空,数组具有空数组元素。我正在尝试取消设置这些元素。

我已经尝试过这些代码片段:

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

但数组总是返回空元素。实际上是否有一种用 PHP 删除空 $_FILES 数组元素的合理方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

送君千里 2024-12-07 23:29:01

当您使用 foreach($_FILES['images']['name'] as $image) 语句 $image 成为数组中实际元素的副本时,您所做的是取消设置该副本,你应该这样做:

foreach( $_FILES['images']['name'] as $key => $value ) {
    if( empty($value) ) {
        unset( $_FILES['images']['name'][$key] );
    }
}

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] );
    }
}
薔薇婲 2024-12-07 23:29:01

首先,您的问题并不具体,因为如果您使用单个文件,则不需要 foreach( ($_FILES['images']['name'] as $image)。
您再次提到表单中的空字段,这应该会触发案例 4 错误。那就是没有上传文件。因此,如果您的错误方法设置如下

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;

,则会通知错误,并且您知道已上传空图像。为了减轻 UNSET() 的压力。
如果是多重上传,你会得到类似的东西

foreach ($_FILES['upload']['name'] as $number => $filename)

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)
情归归情 2024-12-07 23:29:01

这个不循环的答案怎么样?

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

或者,如果您更喜欢一行:

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

来自array_filter

“如果未提供回调,则所有等于 FALSE 的输入条目(请参阅转换为布尔值)将被删除。”

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

梦在夏天 2024-12-07 23:29:01

除了没有人的回答之外,如果您还想从 typetmp_namesize 等数组中删除元素,请使用:

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

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);
黎歌 2024-12-07 23:29:01

使用错误代码是最好的

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

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

    }

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

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文