【问题标题】:Multiple image upload in wordpress在wordpress中上传多张图片
【发布时间】:2015-05-08 03:52:47
【问题描述】:

我想使用 wordpress 上传多张图片。

这是我用于单次上传的代码

 if ( ! function_exists( 'wp_handle_upload' ) ) 

 require_once( ABSPATH . 'wp-admin/includes/file.php' );

$uploadedfile = $_FILES['photo'];

$upload_overrides = array( 'test_form' => FALSE );

$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );


  if ( $movefile ) {

    echo "File is valid, and was successfully uploaded.\n";

    var_dump( $movefile);

   }

 else {

    echo "Possible file upload attack!\n";

   }

如何使用 wordpress 上传多张图片?

【问题讨论】:

    标签: wordpress image-uploading


    【解决方案1】:

    尝试这样实现:

     $files = $_FILES['photo'];
        foreach ($files['photo'] 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】:

      使用原生 WordPress 媒体上传器,您或许可以更轻松地实现您想要做的事情。

      我曾经有一个很好的资源书签,但似乎找不到。几个月前我遇到了需要同样事情的情况。这就是我最终要做的:

      将 WordPress 图像脚本加载到我的 PHP 脚本的开头:

      require_once( ABSPATH . 'wp-admin/includes/image.php' );

      然后你可以像这样使用 jQuery 在元素点击时调用上传器:

      jQuery(document).ready(function($) {
      
        // Define a variable to be used to store the frame data
        var file_frame;
        var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
        var set_to_post_id = 999; // Set this
      
        $('#upload_button').live('click', function( event ){
      
          element = $(this);
      
          event.preventDefault();
      
          // If the media frame already exists, reopen it.
          if (file_frame) {
            // Set the post ID to what we want
            file_frame.uploader.uploader.param('post_id', set_to_post_id);
            // Open frame
            file_frame.open();
            return;
          } else {
            // Set the wp.media post id so the uploader grabs the ID we want when initialised
            wp.media.model.settings.post.id = set_to_post_id;
          }
      
          // Create the media frame.
          file_frame = wp.media.frames.file_frame = wp.media({
            title: $(this).data('uploader_title'),
            button: {
              text: $(this).data('uploader_button_text'),
            },
            multiple: true  // Set to false to allow only one file to be selected
          });
      
          // When an image(s) have been selected, run a callback.
          file_frame.on('select', function() {
      
            // Do something if necessary
              function_to_fire();
      
          });
      
          // Finally, open the modal
          file_frame.open();
        });
      
      });
      

      有一个 attachment 对象传递给回调函数,其中将包含所有 ID 以及上传的附件等。

      set_to_post_id 是您要“附加”媒体的帖子/页面的 ID。如果您不尝试将它们附加到帖子中,则无需担心这一点,并且上述某些代码将不适用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-22
        • 2016-08-25
        • 2019-11-15
        • 1970-01-01
        相关资源
        最近更新 更多