• 黄聪:WordPress: 使用 wp_insert_attachment 上传附件


    我们来研究一下如何在主题或者插件的开发中上传附件:

    主要参考这个函数:https://codex.wordpress.org/Function_Reference/wp_insert_attachment

    下面先主要描述一下流程,假如我们通过一个这个样的表单上传了一张图片:

    <form id="form_register" method="post" enctype="multipart/form-data">
        <input id="upload_image" type="file" name="register_form_image" accept="image/*" />
    </form>

    那么我们在后台可以通过 $_FILE['register_form_image'] 来获取到这个上传的文件:

    array (size=5)
      'name' => string '14249903_1.jpg' (length=14)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'D:upupw	empphpDE28.tmp' (length=25)
      'error' => int 0
      'size' => int 106422

    根据 wp_insert_attachment 这个函数的文档指引,我们需要作如下几步:

    1. 将文件移动到目标的位置
    2. 生成所需的配置,然后调用 wp_insert_attachment
    3. 插入附件的 MetaData

    先做第一步:将文件移动到目标的位置

    我们可以通过调用 wp_upload_dir() 函数来获得目标上传路径的信息,大概是这个样子:

    array (size=6)
      'path' => string 'D:appmysite/wp-content/uploads/2015/05' (length=39)
      'url' => string 'http://mysite/wp-content/uploads/2015/05' (length=39)
      'subdir' => string '/2015/05' (length=8)
      'basedir' => string 'D:appmysite/wp-content/uploads' (length=31)
      'baseurl' => string 'http://mysite/wp-content/uploads' (length=31)
      'error' => boolean false

    然后我们就来把 $_FILES 的临时文件移动过去。

    // 获取上传目录信息
    $wp_upload_dir = wp_upload_dir();
    
    // 将上传的图片文件移动到上传目录
    $basename = $file['name'];
    $filename = $wp_upload_dir['path'].'/'.$basename;
    rename($file['tmp_name'], $filename);

    第二步:指定配置并调用 wp_insert_attachment ,插入attachment 的 post。

    这里的配置有五个参数,第一个是 guid ,这个是外部链接的 url,看下面的代码可以明白;第二个是 post_mime_type ,根据上传时传递的 mime 类型输入即可;第三个是标题,采用去除扩展名之后的文件名;第四个是文章内容,留空,第五个post_status 取值为 inherit;

    // Prepare an array of post data for the attachment.
    $attachment = array(
      'guid'           => $wp_upload_dir['url'] . '/' . $basename,
      'post_mime_type' => $file['type'],
      'post_title'     => preg_replace( '/.[^.]+$/', '', $basename ),
      'post_content'   => '',
      'post_status'    => 'inherit'
    );

    然后是调用 wp_insert_attachment ,注意,第一个参数就是上面的选项数组,第二个参数是第一步指定的文件路径,第三个是这个附件“附加到”的文章 ID;

    // 注意这个是为了说明第三个参数的意义,附件有一个“附加到”的属性,需要从这里指定附加到哪个文章
    // global $post;
    // $parent_post_id = $post->ID;
    
    // 插入附件信息
    $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

    第三步:这里需要有些收尾工作

    参照文档照做即可。

    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    
    // Generate the metadata for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    (转自:http://www.tuicool.com/articles/3yuEBb6)

  • 相关阅读:
    自定义DelegatingHandler为ASP.NET Web Api添加压缩与解压的功能
    搭建Linux环境--部署discuzx论坛
    搭建Linux环境--安装PHP
    搭建Linux环境--安装Apache
    搭建Linux环境--安装MySQL
    搭建Linux环境--安装tomcat
    搭建Linux环境--安装jdk
    vmware中安装了contes--解决ifconfig无法执行
    设计模式(四)原型模式
    设计模式(三)建造者模式
  • 原文地址:https://www.cnblogs.com/huangcong/p/5421807.html
Copyright © 2020-2023  润新知