在自定义上传表单中更改文件名 [英] Change file name in custom upload form

查看:44
本文介绍了在自定义上传表单中更改文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改文件上传名称并添加上传用户的名称.
这是我的函数.php

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;

我尝试使用 $filename = $username .$file_return['文件'];但不工作.

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

推荐答案

wp_handle_upload 接受一个覆盖数组,其中一个参数是 unique_filename_callback,您可以在其中指定一个重命名文件的自定义函数.

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.

尝试这样的事情:

1:在functions.php中添加一个函数,根据需要重命名文件,例如

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:然后在您的upload_user_file() 中,在wp_handle_upload 覆盖中指定您的自定义回调函数,例如

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);

更新:

要在 upload_user_file 函数中获取新文件名,您可以从 wp_handle_upload 返回的 $file_return 数组中的url"中获取它代码>:

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"]);

这篇关于在自定义上传表单中更改文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆