WordPress上传图片自动重命名

Gavin Wu 2020年12月26日 17:12 Wordpress 1,205 Views
1
2
3
4
5
6
7
8
9
10
11
12
13
//WordPress中文名、数字名图片上传自动重命名
add_filter('sanitize_file_name','fanly_custom_upload_name', 5, 1 );
function fanly_custom_upload_name($file){
    $info   = pathinfo($file);
    $ext    = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name   = basename($file, $ext);
    if(preg_match("/[一-龥]/u",$file)){//中文换名
        $file   = substr(md5($name), 0, 20) . rand(00,99) . $ext;//截取前20位MD5长度,加上两位随机
    }elseif(is_numeric($name)){//数字换名
        $file   = substr(md5($name), 0, 20) . rand(00,99) . $ext;//截取前20位MD5长度,加上两位随机
    }
    return $file;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Top