使用MEDIA_HANDLE_UPLOAD上传多个文件

时间:2014-12-25 作者:Engr.MTH

我有一个WordPress表单插件,我使用media_handle_upload 为了上传文件并直接获取ID并将其ID作为元日期附加到帖子中,我使用了以下方法:

表单字段的HTML为:

<input type="file" name="my_file_upload" id="my_file_upload">
php代码是:

$attach_id = media_handle_upload( \'my_file_upload\', $post_id );
if ( is_numeric( $attach_id ) ) {
    update_post_meta( $post_id, \'_my_file_upload\', $attach_id );
}
一切都很顺利。

现在我正在尝试上载多个文件我的HTML代码是:

<input type="file" name="my_file_upload[]" id="my_file_upload[]" multiple="multiple">
但我不能media_handle_upload 函数用于多个文件上载。

任何帮助都将不胜感激。

5 个回复
最合适的回答,由SO网友:Shady M Rasmy 整理而成

如果您在开始时使用自定义模板超过此值

<?php
 if( \'POST\' == $_SERVER[\'REQUEST_METHOD\']  ) {
if ( $_FILES ) { 
    $files = $_FILES["my_file_upload"];  
    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]
                ); 
                $_FILES = array ("my_file_upload" => $file); 
                foreach ($_FILES as $file => $array) {              
                    $newupload = my_handle_attachment($file,$pid); 
                }
            } 
        } 
    }

}
?>
在功能中。php

function my_handle_attachment($file_handler,$post_id,$set_thu=false) {
  // check to make sure its a successful upload
  if ($_FILES[$file_handler][\'error\'] !== UPLOAD_ERR_OK) __return_false();

  require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
  require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
  require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');

  $attach_id = media_handle_upload( $file_handler, $post_id );
  if ( is_numeric( $attach_id ) ) {
    update_post_meta( $post_id, \'_my_file_upload\', $attach_id );
  }
  return $attach_id;
}
资源http://www.kvcodes.com/2013/12/create-front-end-multiple-file-upload-wordpress/

SO网友:Lucky

如果希望在不使用函数文件的情况下实现此功能,可以使用我提出的简化版本。这是测试代码100%工作

<form id="file_upload" method="post" action="#" enctype="multipart/form-data">
     <input type="file" name="my_file_upload[]" multiple="multiple">
     <input name="my_file_upload" type="submit" value="Upload" />
</form>
将PHP代码放在发生提交的页面上。在我的情况下,表单操作设置为“#”

<?php if ($_SERVER[\'REQUEST_METHOD\'] == \'POST\') {
    require_once( ABSPATH . \'wp-admin/includes/image.php\' );
    require_once( ABSPATH . \'wp-admin/includes/file.php\' );
    require_once( ABSPATH . \'wp-admin/includes/media.php\' );

    $files = $_FILES["my_file_upload"];
    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]
            );
            $_FILES = array("upload_file" => $file);
            $attachment_id = media_handle_upload("upload_file", 0);

            if (is_wp_error($attachment_id)) {
                // There was an error uploading the image.
                echo "Error adding file";
            } else {
                // The image was uploaded successfully!
                echo "File added successfully with ID: " . $attachment_id . "<br>";
                echo wp_get_attachment_image($attachment_id, array(800, 600)) . "<br>"; //Display the uploaded image with a size you wish. In this case it is 800x600
            }
        }
    }
} ?>
当表单提交完成时,此方法只包含一次所需的文件,而不是每次通过foreach循环调用函数时都包含它们

SO网友:Sandrine

感谢@shady-m-rasmyI使用了您提到的代码,第二个foreach循环(在自定义模板部分的下面)似乎没有必要,因为它只执行一次。

foreach ($_FILES as $file => $array) {              
   $newupload = my_handle_attachment($file,$pid);
} 
所以它只剩下

$newupload = my_handle_attachment( "my_file_upload", $pid);

SO网友:Mbeche Esikanda

同一元密钥的多个条目

$values = [ \'red\', \'yellow\', \'blue\', \'pink\' ];
foreach( $values as $value ) {
    // This method uses `add_post_meta()` instead of `update_post_meta()`
    add_post_meta( $item_id, \'color\', $value );
}

SO网友:Maximilian Meyer

HTML

<input type="file" id="id_termine_attachment" multiple="multiple" name="id_termine_attachment[]" value="" size="25" />
PHP

function upload_file($_FILES) {

    if (!empty($_FILES[\'id_termine_attachment\'])) {
        $supported_types = array(
            \'application/pdf\',
            \'application/vnd.openxmlformats-officedocument.wordprocessingml.document\',
            \'application/msword\',
            \'image/gif\',
            \'image/jpeg\',
            \'image/png\',
            \'application/zip\'
         );

        $file_arr = reArrayFiles($_FILES[\'id_termine_attachment\']);
        $file_urls = [];

        foreach ($file_arr as $file) {
            $arr_file_type = wp_check_filetype(basename($file[\'name\']));
            $uploaded_type = $arr_file_type[\'type\'];
            if (in_array($uploaded_type, $supported_types)) {
                $upload = wp_upload_bits($file[\'name\'], null, file_get_contents($file[\'tmp_name\']));
                if (isset($upload[\'error\']) && $upload[\'error\'] != 0) {
                    wp_die(\'There was an error uploading your file. The error is: \' . $upload[\'error\']);
                } else {
                    array_push($file_urls, $upload[\'url\']);

                } // end if/else
            } else {
                wp_die("This filetyp is not available!");
            }
        }
        update_post_meta($post_id, \'id_termine_attachment\', $file_urls);
    }
}



function reArrayFiles(&$file_post) {
    $file_ary = array();
    $file_count = count($file_post[\'name\']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}

结束

相关推荐

Extending the Audio Shortcode

目前,音频快捷码只允许四个属性,src, loop, autoplay 和preload. 然而,当你上传一个音频文件时,它附带了非常有用的元数据,如专辑的艺术、艺术家、年份等,如果它也能显示出来,那就太好了。我一直在寻找一种扩展音频短代码的方法,以便元数据也可以包含在短代码中。到目前为止,我偶然发现shortcode_atts_{$shortcode} 它可以用来过滤现有的短代码,但显然只能过滤现有属性,不能添加新属性。顺便说一句,我并不想创建一个新的短代码,而是要添加或扩展现有的短代码,这样用户就不必