Change file name in custom upload form

2019-06-14 11:35发布

i want change file upload name and add the name of user that upload it.
This my function.php

if ( ! function_exists( 'upload_user_file' ) ) :
    function upload_user_file( $file = array(), $title = false ) {
        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{

            $user = wp_get_current_user();
            $username = $user->display_lastname;
            $filename  =  $username . $file_return['file'];

     return $filename;
            $attachment = array(
                'post_mime_type' => $file_return['type'],
                'post_content' => '',
                'post_type' => 'attachment',
                'post_status' => 'inherit',
                'guid' => $file_return['url']
            );

            if($title){
                $attachment['post_title'] = $title;

            }
            $attachment_id = wp_insert_attachment( $attachment, $filename );

            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;
    }
endif;

i try with $filename = $username . $file_return['file']; but don't work.

标签: php wordpress
1条回答
虎瘦雄心在
2楼-- · 2019-06-14 11:44

wp_handle_upload accepts an array of overrides, and one of the arguments is unique_filename_callback where you can specify a custom function to rename the file.

Try something like this:

1: Add a function to functions.php to rename the file as you want it, e.g.

function my_custom_filename($dir, $name, $ext){
    $user = wp_get_current_user();

    /* You wanted to add display_lastname, but its not required by WP so might not exist. 
       If it doesn't use their username instead: */
    $username = $user->display_lastname;
    if (!$username)  $username = $user->user_login;

    $newfilename =  $username ."_". $name;  /* prepend username to filename */

    /* any other code you need to do, e.g. ensure the filename is unique, remove spaces from username etc */

    return $newfilename;
}

2: Then in your upload_user_file(), specify your custom callback function in the wp_handle_upload overrides, e.g.

    $overrides = array( 
        'test_form' => false, /* this was in your existing override array */
        'unique_filename_callback' => 'my_custom_filename' 
    );

    /* pass your overrides array into wp_handle_upload */
    $file_return = wp_handle_upload($file,$overrides);

UPDATE:

To get the new filename in the upload_user_file function, you can get it from the "url" in the $file_return array that's returned by wp_handle_upload:

$newfilename = basename($file_return["url"]);
查看更多
登录 后发表回答