php - 如何获取自定义帖子类型附件的文件名和文件大小

标签 php wordpress custom-post-type

我正在制作一个名为circular的自定义post_type插件,我使用元框上传PDF或图像文件,现在我可以检索文件的url,但如何获取文件名和来 self 的自定义 post_type 元字段的大小。

这是我的元框代码

function add_custom_meta_boxes() {

    // Define the custom attachment for posts
    add_meta_box(
        'wp_custom_attachment',
        'Custom Attachment',
        'wp_custom_attachment',
        'circular',
        'side'
    );

} // end add_custom_meta_boxes
add_action('add_meta_boxes', 'add_custom_meta_boxes');

function wp_custom_attachment() {

    wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');

    $html = '<p class="description">';
        $html .= 'Upload your PDF here.';
    $html .= '</p>';
    $html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25" />';

    echo $html;

} // end wp_custom_attachment


function save_custom_meta_data($id) {

    /* --- security verification --- */
    if(!wp_verify_nonce($_POST['wp_custom_attachment_nonce'], plugin_basename(__FILE__))) {
      return $id;
    } // end if

    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
      return $id;
    } // end if

    if('circular' == $_POST['post_type']) {
      if(!current_user_can('edit_page', $id)) {
        return $id;
      } // end if
    } else {
        if(!current_user_can('edit_page', $id)) {
            return $id;
        } // end if
    } // end if
    /* - end security verification - */

    // Make sure the file array isn't empty
    if(!empty($_FILES['wp_custom_attachment']['name'])) {

        // Setup the array of supported file types. In this case, it's just PDF.
        $supported_types = array('application/pdf');

        // Get the file type of the upload
        $arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_attachment']['name']));
        $uploaded_type = $arr_file_type['type'];

        // Check if the type is supported. If not, throw an error.
        if(in_array($uploaded_type, $supported_types)) {

            // Use the WordPress API to upload the file
            $upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name']));

            if(isset($upload['error']) && $upload['error'] != 0) {
                wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
            } else {
                add_post_meta($id, 'wp_custom_attachment', $upload);
                update_post_meta($id, 'wp_custom_attachment', $upload);     
            } // end if/else

        } else {
            wp_die("The file type that you've uploaded is not a PDF.");
        } // end if/else

    } // end if

} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');

function update_edit_form() {
    echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');

输出该文件的链接

<?php $img = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); ?>
<a href="<?php echo $img['url']; ?>"> Download PDF Here</a>

最佳答案

首先需要获取文件 url,我们可以获取大小和名称。这里 wp_custom_attachment 是自定义字段 ID。

 // retrieve file of the custom field 
 $file = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); 

 //get the url
 $url = $file['url'];

 //Replace url to directory path
  $path = str_replace( site_url('/'), ABSPATH, esc_url( $url) );

  if ( is_file( $path ) ){
    $filesize = size_format( filesize( $path ) );
    $filename = basename($path); 
  }

            echo '<p>Name: ' . $filename . '</p>';
            echo '<p>Size: ' . $filesize . '</p>';

关于php - 如何获取自定义帖子类型附件的文件名和文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47272813/

相关文章:

javascript - 如何通过URL防止XSS?

javascript - Stripe 付款按钮没有响应

WordPress - 获取每个用户的最新自定义帖子

wordpress - 在自定义帖子中上传多张精选图片 (Wordpress)

php - wordpress 自定义帖子类型中的字段验证和显示错误

php - 自动 <p> 正则表达式 - 需要修复

php - "Call to undefined function mysql_connect()"升级到 php-7 后

php - Laravel 数组而不是集合

php - 从 Excel 文件获取信息然后导入到 CSS

css - 自定义 WP 主题 - 如何更改 CSS 中导航按钮的链接?