PHP-上传多个文件 [英] PHP - Uploading multiple files

查看:73
本文介绍了PHP-上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为wordpress制作插件,我希望能够从表单上传多张图片.现在,当我有一张包含两张图片的表单并将其提交为空时,我的$ _FILES数组如下所示:

I'm working on a plugin for wordpress and I want to be able to upload multiple pictures from a form. Right now when I have a form for two pictures and submit it empty, my $_FILES array looks like this:

Array (
    [image] => Array ( 
        [name] => Array ( 
            [1] => 
            [2] =>
        ) 
        [type] => Array ( 
            [1] => 
            [2] =>
        ) 
        [tmp_name] => Array ( 
            [1] => 
            [2] =>
        ) 
        [error] => Array ( 
            [1] => 4 
            [2] => 4
        ) 
        [size] => Array ( 
            [1] => 0 
            [2] => 0 
        ) 
    ) 
)

现在的问题是我想使用wordpress的上载处理程序wp_handle_upload.它期望将$ _FILES数组作为参数,但仅包含一个文件.我猜它只能是两个数组深,不能像我的那样三个.所以我想知道是否有一种方法可以一次从$ _FILES数组提交一个文件.这些文件在每个数组中具有相同的密钥.

Now the problem is that I want to use wordpress' upload handler, wp_handle_upload. It expects the $_FILES array as an argument, but only with one file. I guess it can only be two arrays deep, not three as mine is. So I'm wondering if there's a way to submit the files one at a time from the $_FILES array. The files have the same key in each array.

因为我得知wp_handle_upload希望将$ _FILES数组作为参数,所以更改了帖子.

Changed the post since I learned that the wp_handle_upload wants the $_FILES array as argument.

推荐答案

尝试:

$files = $_FILES['image'];
foreach ($files['name'] as $key => $value) {
  if ($files['name'][$key]) {
    $file = array(
      'name'     => $files['name'][$key],
      'type'     => $files['type'][$key],
      'tmp_name' => $files['tmp_name'][$key],
      'error'    => $files['error'][$key],
      'size'     => $files['size'][$key]
    );
    wp_handle_upload($file);
  }
}

这篇关于PHP-上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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