PHP - 上传多个文件

标签 php wordpress plugins forms upload

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

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 数组中一次提交一个文件。这些文件在每个数组中具有相同的键。

编辑:更改了帖子,因为我了解到 wp_handle_upload 需要 $_FILES 数组作为参数。

最佳答案

尝试:

$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 - 上传多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4178873/

相关文章:

shell - 在 cakephp 2.0 中运行插件的 shell

javascript - 从插件的辅助功能访问 jQuery 对象

PHP/MySQL更新数据库中一条消息记录的问题

php - 如何使用中文版 mPDF

html - 如何让产品 (Woocommerce) 的标题以白色文本显示

php - ajax 帖子未返回成功

php - 如何使用 PHP 自定义查询从 Wordpress 数据库表 wp_postMeta 中获取数据

php - 如何在特定日期在 php 中发送?

css - 菜单环绕和空白 :nowrap making the menu go past the page Wordpress

ios/iphone - [NSNull dataUsingEncoding :]: unrecognized selector sent to instance