WordPress-如何检索输入类型=";文件";?

WordPress-如何检索输入类型=";文件";?,wordpress,Wordpress,我已经用将制作成Wordpress,我想检索此文件以使用它$\u POST[“文件”]只给了我文件名。 我可以将这些文件保存到Wordpress的媒体库中 你知道我该怎么做吗 谢谢 保存文件或图像的php文件 <?php // Check that the nonce is valid, and the user can edit this post. if ( isset( $_POST['my_image_upload_nonce'], $_POST['post_id']

我已经用
制作成Wordpress,我想检索此文件以使用它<代码>$\u POST[“文件”]只给了我文件名。 我可以将这些文件保存到Wordpress的媒体库中

你知道我该怎么做吗


谢谢

保存文件或图像的php文件

<?php

// Check that the nonce is valid, and the user can edit this post.
if ( 
    isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] ) 
    && wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
    && current_user_can( 'edit_post', $_POST['post_id'] )
) {
    // The nonce was valid and the user has the capabilities, it is safe to continue.

    // These files need to be included as dependencies when on the front end.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    // Let WordPress handle the upload.
    // Remember, 'my_image_upload' is the name of our file input in our form above.
    $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }

} else {

    // The security check failed, maybe show the user an error.
}
if(isset($_FILES)){ 
        foreach( $_FILES as $file ) 
        {
            if( is_array( $file ) ) {
                $attachment_id = upload_user_file( $file );                //Call function 
                //add your code to use attachment id
            }
        }  
}
当前激活主题的function.php

function upload_user_file( $file = array() ) {    
    require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    $file_return = wp_handle_upload( $file, array('test_form' => false ) );
    if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
        return false;
    } else {
        $filename = $file_return['file'];
        $attachment = array(
            'post_mime_type' => $file_return['type'],
            'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
            'post_content' => '',
            'post_status' => 'inherit',
            'guid' => $file_return['url']
        );
        $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
        wp_update_attachment_metadata( $attachment_id, $attachment_data );
        if( 0 < intval( $attachment_id ) ) {
          return $attachment_id;
        }
    }
    return false;
}
函数上载用户文件($file=array()){
require_once(ABSPATH.'wp admin/includes/admin.php');
$file\u return=wp\u handle\u upload($file,array('test\u form'=>false));
if(isset($file_return['error'])| | isset($file_return['upload_error_handler'])){
返回false;
}否则{
$filename=$file_return['file'];
$attachment=array(
'post_mime_type'=>$file_return['type'],
'post\u title'=>preg\u replace('/\.[^.]+$/','',basename($filename)),
'发布内容'=>'',
“post_status”=>“inherit”,
'guid'=>$file\u返回['url']
);
$attachment_id=wp_insert_attachment($attachment,$file_return['url']);
require_once(ABSPATH.'wp admin/includes/image.php');
$attachment\u data=wp\u generate\u attachment\u元数据($attachment\u id,$filename);
wp_更新_附件_元数据($attachment_id,$attachment_data);
如果(0
在php中,您必须访问
$\u文件
才能访问文件类型,您不能将文件名放入
$\u POST
。阅读
function upload_user_file( $file = array() ) {    
    require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    $file_return = wp_handle_upload( $file, array('test_form' => false ) );
    if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
        return false;
    } else {
        $filename = $file_return['file'];
        $attachment = array(
            'post_mime_type' => $file_return['type'],
            'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
            'post_content' => '',
            'post_status' => 'inherit',
            'guid' => $file_return['url']
        );
        $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
        wp_update_attachment_metadata( $attachment_id, $attachment_data );
        if( 0 < intval( $attachment_id ) ) {
          return $attachment_id;
        }
    }
    return false;
}