一般情况下,WordPress是默认不会对上传文件进行改名,这样同名文件会不断被加后缀,那么如何对WordPress上传的图片进行改名并加上时间戳?

另一方面,如果文件名里面有中文字符的话,还有可能出现乱码的情况。比如中文字符后缀的就可能出现上传失败。

现在提供一个钩子,让上传文件时自动改名

/**

* //wordpress对上传的图片重名,加上时间戳

*

*/

add_filter( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’wp_handle_upload_prefilter\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, function($file){

// print_r($file);

$info = pathinfo($file[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]);

// print_r($info);

//显示带有文件扩展名的文件名

$filename = $file[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’filename\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’];

$ext = empty($info[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’extension\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]) ? \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ : \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ . $info[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’extension\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’];

$file[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] = md5($filename.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’-\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.time()).$ext;

//文件名称小于15位时,才改名

// if(strlen($file[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’])<=15)

// {

// $file[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] = time().\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’-\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.$file[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’];

//$file[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] = substr(md5($file[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]), 0, 6).\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’-\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.time();

// }

return $file;

});

以上就是WordPress主题站小编为大家分享的对WordPress上传的图片进行改名并加上时间戳的方法。

相关新闻

发表评论

您的电子邮箱地址不会被公开。

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部