PHP - Uploading multiple files

霸气de小男生 提交于 2019-12-05 00:42:13

问题


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 
        ) 
    ) 
)

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.

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


回答1:


Try:

$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);
  }
}



回答2:


Couldn't you loop through your files array and then call the upload_handlers?

e.g.

for( $i = 1; $i <= count( $_FILES['image']['name']; $i++ ) {
    // just cguessing on the args wp_handle_upload takes
    wp_handle_upload( $_FILES['images']['tmp'][$i], $_FILES['images']['name'][$i] );
}


来源:https://stackoverflow.com/questions/4178873/php-uploading-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!