使用 PHP 上传 HTML 5 多文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3215324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:02:02  来源:igfitidea点击:

HTML 5 multi file upload with PHP

phphtmlmultifile

提问by kylex

Here is the code I have, and I'm wondering what I'm doing wrong that it doesn't display the name.

这是我的代码,我想知道我做错了什么,它没有显示名称。

<form action = "self.php" method="post" enctype="multipart/form-data">
<input type="file" name="imageURL[]" id="imageURL" multiple="" />
<input type="submit" value="submit" name="submit" />
</form>

And the processing info that isn't working:

以及不起作用的处理信息:

foreach ($_FILES['imageURL'] as $files[]) { 
    echo $files['file'];
}

Edit:

编辑:

When changing my foreach loop to:

将我的 foreach 循环更改为:

foreach ($_FILES['imageURL'] as $file) { 
    echo $file['name'];
}

Still nothing prints out.

仍然没有打印出来。

However, when I do something like this:

但是,当我做这样的事情时:

foreach ($_FILES['imageURL']['name'] as $filename) { 
    echo $filename;
}

The filename does print. I don't know what that implies though.

文件名确实打印。我不知道这意味着什么。



SOLVED UPDATE:



解决更新:

As linked to by John Conde, the array interlace structure is different when uploading multiple files than when uploading a single file. To use foreach, we must restructure the array.

正如 John Conde 所链接的,上传多个文件时的数组交错结构与上传单个文件时的不同。要使用 foreach,我们必须重构数组。

$files=array();
$fdata=$_FILES['imageURL'];
if(is_array($fdata['name'])){
for($i=0;$i<count($fdata['name']);++$i){
        $files[]=array(
    'name'    =>$fdata['name'][$i],
    'type'  => $fdata['type'][$i],
    'tmp_name'=>$fdata['tmp_name'][$i],
    'error' => $fdata['error'][$i], 
    'size'  => $fdata['size'][$i]  
    );
    }
}else $files[]=$fdata;

NOW we can use foreach to loop:

现在我们可以使用 foreach 来循环:

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

回答by John Conde

Try

尝试

foreach ($_FILES['imageURL'] as $file) { 
    echo $file['name'];
}

UPDATE:

更新:

Google found this tutorialwhich may help you

谷歌发现这个教程可以帮助你

回答by kotekzot

Instead of using for() and recounting the number of items in the array, you can use a more elegant foreach()

您可以使用更优雅的 foreach(),而不是使用 for() 并重新计算数组中的项目数

$files=array();
$fdata=$_FILES['file'];
if(is_array($fdata['name'])){
    foreach ($fdata['name'] as $i => $d) {
        $files[] = array(
        'name' => $d,
        'tmp_name' => $fdata['tmp_name'][$i]
        );
    }
}
else $files[]=$fdata;

回答by Powerlord

Maybe I'm wrong, but wouldn't setting multiple=""turn multiple uploads off? Just use multipleby itself, as shown in the HTML5 specor, for XHTML compatibility, multiple="multiple":

也许我错了,但是设置不会multiple=""关闭多个上传吗?只需单独使用multiple,如HTML5 规范中所示,或者,对于 XHTML 兼容性,multiple="multiple"

<input type="file" name="imageURL[]" id="imageURL" multiple />