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

Php 如何获取自定义帖子类型附件的文件名和文件大小,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我正在制作一个名为circular的自定义post_类型插件。我使用一个元框上载PDF或图像文件。现在我可以检索文件的url,但如何从自定义post_类型元字段获取文件名和大小 这是我的元框代码 function add_custom_meta_boxes() { // Define the custom attachment for posts add_meta_box( 'wp_custom_attachment', 'Custom Atta

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

这是我的元框代码

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');
函数添加自定义元框(){
//定义帖子的自定义附件
添加元框(
“wp_自定义_附件”,
“自定义附件”,
“wp_自定义_附件”,
"通告",,
“一边”
);
}//结束添加\自定义\元\框
添加动作(“添加元框”、“添加自定义元框”);
函数wp_自定义_附件(){
wp_nonce_字段(plugin_basename(_文件___;)、wp_custom_attachment_nonce);
$html='

'; $html.='在此处上载您的PDF'; $html.='

'; $html.=''; echo$html; }//结束wp\u自定义\u附件 函数保存自定义元数据($id){ /*---安全验证--*/ 如果(!wp\u verify\u nonce($\u POST['wp\u custom\u attachment\u nonce'],plugin\u basename(\uuuu FILE\uuuu))){ 返回$id; }//如果结束,则结束 if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE){ 返回$id; }//如果结束,则结束 如果('circular'=$\u POST['POST\u type'])){ 如果(!当前用户可以('edit_page',$id)){ 返回$id; }//如果结束,则结束 }否则{ 如果(!当前用户可以('edit_page',$id)){ 返回$id; }//如果结束,则结束 }//如果结束,则结束 /*-结束安全验证-*/ //确保文件数组不是空的 如果(!空($\u文件['wp\u自定义\u附件]['name'])){ //设置支持的文件类型数组。在本例中,它只是PDF。 $supported_types=数组('application/pdf'); //获取上载的文件类型 $arr_file_type=wp_check_文件类型(basename($_FILES['wp_custom_attachment']['name']); $uploaded_type=$arr_文件_type['type']; //检查该类型是否受支持。如果不受支持,则抛出错误。 if(在_数组中($upload_type,$supported_type)){ //使用WordPress API上传文件 $upload=wp\u上传\u位($文件['wp\u自定义\u附件]['name'],空,文件内容($文件['wp\u自定义\u附件]['tmp\u名称]]); 如果(isset($upload['error'])&&$upload['error']!=0){ wp_die('上载文件时出错。错误为:'.$upload['error']); }否则{ 添加帖子元($id,'wp\u自定义附件',$upload); 更新发布元数据($id,'wp\u自定义附件',$upload); }//如果结束/else }否则{ wp_die(“您上传的文件类型不是PDF”); }//如果结束/else }//如果结束,则结束 }//结束保存自定义元数据 添加操作(“保存帖子”、“保存自定义元数据”); 函数更新\编辑\表单(){ echo'enctype=“多部分/表单数据”; }//结束更新\u编辑\u表单 添加动作(“发布编辑表单标签”、“更新编辑表单”);
请将该文件的链接放出来

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

如果您可以获取附件ID号($attachment\u 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>';
$attachment_id='YOUR_PDF_id';
$attachment\u file=获取附加的文件($attachment\u id);
函数getSize($file){
$bytes=文件大小($file);
$s=数组('b','Kb','Mb','Gb');
$e=楼层(日志($bytes)/log(1024));
返回sprintf('%.2f'.$s[$e],($bytes/pow(1024,floor($e)));
}
回显“名称:”。basename($attachment_file)。'

",; 回显“大小:”。getSize($attachment_文件)。'

",;

我在另一篇文章中找到了“getSize”函数。在匹配WP media library meta中显示的大小方面,它比使用本机PHP“filesize”函数更准确。

首先需要获取文件url,我们可以获取大小和名称。此处
wp\u custom\u附件
是自定义字段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>';
//检索自定义字段的文件
$file=get_post_meta(get_ID(),'wp_custom_attachment',true);
//获取url
$url=$file['url'];
//将url替换为目录路径
$path=str_replace(site_url('/')、ABSPATH、esc_url($url));
if(is_文件($path)){
$filesize=size_格式(filesize($path));
$filename=basename($path);
}
回显“名称:”$文件名

",; 回显“大小:”$文件大小。”

",;
哪个部件不工作?我相信我在发布之前测试了代码,你能提供更多信息让我帮助调试吗?我用过你的代码,但是它返回一个空值不确定为什么它可能是一个自定义的发布类型?或者在您的代码中,
$attachment\u文件
应该是一个数组,并且
getSize($file)
这里,$file没有值,我试图修改您的代码,但失败了。但下面的答案是完美的。感谢支持人员。在您的
get\u post\u meta()
中,
$single
参数设置为true,返回“”。我认为您的
$file
变量可能是图像ID号。你能试试
var\u dump($file)
那里看看
get\u post\u meta()
返回的是什么吗?
size\u format
是正确返回文件大小的函数你试过我在前面的回答中提到的
getSize()
函数了吗??