你如何遍历 $_FILES 数组? [英] How do you loop through $_FILES array?

查看:33
本文介绍了你如何遍历 $_FILES 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想循环的输入

Main photo:   <input type="file" name="image[]" />
Side photo 1: <input type="file" name="image[]" />
Side photo 2: <input type="file" name="image[]" />
Side photo 3: <input type="file" name="image[]" />

发生了一些奇怪的事情,当我没有上传任何内容时,我使用 count($_FILES['image']),我回应了该函数,它返回了 5 的值.应该有该数组中没有元素.当我只有 4 个文件时,为什么会有一个额外的输入?

A couple weird things happened, when I uploaded nothing I use the count($_FILES['image']), I echoed that function, and it returns a value of 5. There should be no elements in that array. Why is there one extra input when I only have 4 files to begin with?

现在实际循环本身,我尝试使用 foreach 循环,但它不起作用.

Now with the actually looping itself, I try using the foreach loop, but it doesn't work.

foreach($_FILES['image'] as $files){echo $files['name']; }

什么都没发生,我最终想做的是遍历所有图像,确保它们的格式、大小正确,并重命名每个图像.但是这个简单的 foreach() 循环表明,不知何故,我什至无法遍历 $_FILES 数组,而当我什至没有上传任何内容时,当它说数组中有 5 个元素时,count() 使我更加困惑.

Nothing came up, what I wanted to ultimately do is to loop through all images, make sure they are correct format, size, and rename each of them. But this simple foreach() loop shows that somehow I can't even loop through the $_FILES array and the count() confused me even more when it say that there are 5 elements in the array when I didn't even upload anything.

推荐答案

您的示例表单应该可以正常工作.只是您期望 $_FILES 超全局变量的结构与实际不同,当使用数组结构作为字段名称时.

Your example form should work fine. It's just that you are expecting the structure of the $_FILES superglobal to be different than it actually is, when using an array structure for the field names.

这个多维数组的结构如下:

The structure of this multidimensional array is as followed then:

$_FILES[fieldname] => array(
    [name] => array( /* these arrays are the size you expect */ )
    [type] => array( /* these arrays are the size you expect */ )
    [tmp_name] => array( /* these arrays are the size you expect */ )
    [error] => array( /* these arrays are the size you expect */ )
    [size] => array( /* these arrays are the size you expect */ )
);

因此 count( $_FILES[ "fieldname" ] ) 将产生 5.
但是计算更深的维度也不会产生您可能期望的结果.例如,使用 count( $_FILES[ "fieldname" ][ "tmp_name" ] ) 计算字段将始终得到文件字段的数量,而不是实际上传的文件数量.您仍然需要遍历元素以确定是否已为特定文件字段上传任何内容.

Therefor count( $_FILES[ "fieldname" ] ) will yield 5.
But counting deeper dimensions will also not produce the result you may expect. Counting the fields with count( $_FILES[ "fieldname" ][ "tmp_name" ] ) for instance, will always result in the number of file fields, not in the number of files that have actually been uploaded. You'd still have to loop through the elements to determine whether anything has been uploaded for a particular file field.

编辑
因此,要遍历字段,您可以执行以下操作:

EDIT
So, to loop through the fields you would do something like the following:

// !empty( $_FILES ) is an extra safety precaution
// in case the form's enctype="multipart/form-data" attribute is missing
// or in case your form doesn't have any file field elements
if( strtolower( $_SERVER[ 'REQUEST_METHOD' ] ) == 'post' && !empty( $_FILES ) )
{
    foreach( $_FILES[ 'image' ][ 'tmp_name' ] as $index => $tmpName )
    {
        if( !empty( $_FILES[ 'image' ][ 'error' ][ $index ] ) )
        {
            // some error occured with the file in index $index
            // yield an error here
            return false; // return false also immediately perhaps??
        }

        /*
            edit: the following is not necessary actually as it is now 
            defined in the foreach statement ($index => $tmpName)

            // extract the temporary location
            $tmpName = $_FILES[ 'image' ][ 'tmp_name' ][ $index ];
        */

        // check whether it's not empty, and whether it indeed is an uploaded file
        if( !empty( $tmpName ) && is_uploaded_file( $tmpName ) )
        {
            // the path to the actual uploaded file is in $_FILES[ 'image' ][ 'tmp_name' ][ $index ]
            // do something with it:
            move_uploaded_file( $tmpName, $someDestinationPath ); // move to new location perhaps?
        }
    }
}

有关详细信息,请参阅文档.

For more information see the docs.

这篇关于你如何遍历 $_FILES 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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