阵列$ _FILES上载的未设定项目 [英] Unset item from Array $_FILES upload

查看:42
本文介绍了阵列$ _FILES上载的未设定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,用户可以在其中上传多个文件(最多8个).HTML由一段PHP生成:

I have a script where users can upload multiple files (max. 8). The HTML is generated by a piece of PHP:

$max_no_img=8;  
for($i=1; $i<=$max_no_img; $i++){
    <div class='photo photo$i'>
        <div class='new_label'>
        Foto $i:
        </div>
        <div class='new_input'>
        <input type='file' name='images[]' />
        </div>
    </div>";
}

因此数组 images [] 由8个值组成.但是,每次用户提交表单时,该表单都会生成一个由8个项目组成的数组,并因此在数据库中插入8个值(无论它们是否为空).

So the array images[] consists of 8 values. However, every time a user submit it's form, the form is generating an Array of 8 items and by so, inserting 8 values in the database (whether they are empty or not).

因此,我想取消设置空值,将文件复制到我的文件夹中,然后将链接插入到我的数据库中.但是,这里是发生错误的部分.

So I would like to unset the empty values, copy the files to my folders and insert the link into my database. But here is the part where the errors happen.

$ _ FILES 的数组由4个元素组成. name tmp_name error size .我如何获取它,以便从数组中取消设置完整的项目(例如 images [0] ),因此我可以继续处理实际带有值的项目.

The array of $_FILES consists of 4 things. name, tmp_name, error and size. How do I get it so a complete item (let's say images[0]) will be unset from the array so I can continue with the items which actually carry a value.

我尝试过,但是没有结果...

I tried this, but with no results...

unset($_FILES['images'][0])

unset($_FILES['images']['name'][0])
unset($_FILES['images']['tmp_name'][0])
unset($_FILES['images']['error'][0])
unset($_FILES['images']['size'][0])

有人建议如何从$ _FILES-arry中取消设置一个值吗?

Any advice how to unset a value from a $_FILES-arry?

推荐答案

您可以忽略它们,而不必处理或取消设置任何项目:

You can just ignore them instead of processing or unsetting any items:

if (!empty($_FILES['images'])) {
    for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
        if (empty($_FILES['images']['name'][$i])) {
            // This item is empty
            echo "Item $i references an empty field.\n";
            continue;
        }

        echo "Item $i is a valid file.\n";
    }
}

这篇关于阵列$ _FILES上载的未设定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆