【问题标题】:How to get the file name and file size for the attachment of my custom post type如何获取我的自定义帖子类型附件的文件名和文件大小
【发布时间】:2018-04-26 15:15:54
【问题描述】:

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

这是我的 Meta Box 代码

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>

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    如果您可以获取附件 ID 号(下面的 $attachment_id),您应该可以执行以下操作来获取名称/大小:

    $attachment_id = 'YOUR_PDF_ID';
    $attahment_file = get_attached_file( $attachment_id );
    
    function getSize($file) {
        $bytes = filesize($file);
        $s = array('b', 'Kb', 'Mb', 'Gb');
        $e = floor(log($bytes)/log(1024));
        return sprintf( '%.2f ' . $s[$e], ( $bytes/pow( 1024, floor($e) ) ) );
    }
    
    echo '<p>Name: ' . basename( $attahment_file ) . '</p>';
    echo '<p>Size: ' . getSize( $attahment_file ) . '</p>';
    

    我在另一个帖子here 上找到了“getSize”功能。在匹配 WP 媒体库元数据中显示的大小方面,它比使用原生 PHP“filesize”函数更准确。

    【讨论】:

    • 哪个部分不工作?我相信我在发布之前测试了代码,您能提供更多信息以便我帮助调试吗?
    • 我使用了您的代码,但它返回一个空值,不确定为什么它可能是自定义帖子类型?或在您的代码中$attahment_file 应该是一个数组,getSize($file) 在这里 $file 没有价值我尝试修改您的代码但失败了。但是下面的答案是完美的。感谢大佬的支持。
    【解决方案2】:

    首先需要获取文件的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>';
    

    【讨论】:

    • get_post_meta() 中,$single 参数设置为 true,返回“value of meta data field”。我想你 $file 变量可能是图像 ID 号。你可以试试var_dump($file) 看看get_post_meta() 会返回什么吗?
    • size_format 是正确返回文件大小的函数
    • 你试过我之前回答中提到的getSize()函数了吗??
    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2013-08-13
    • 2020-05-14
    • 1970-01-01
    相关资源
    最近更新 更多