WordPress 中文图片 上传 自动重命名

由于国人很少有在上传图片前将图片名重命名为英语的,所以自动重命名对于WP来说尤为重要,特别是LINUX的不支持中文名的。

WordPress上传多媒体的代码都存放于\wp-admin\includes\里面的file.php,打开这个文件,$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );// Move the file to the uploads dir,在此句$filename赋值前添加代码如下:

$time=date("YmdHis");$type_change = array('image'=>'.');$file_type = strtr($file['type'],$type_change);
$file['name'] = $time."".mt_rand(1,100)."".$file_type ;

之后代码如下:

    $time=date("YmdHis");$type_change = array('image'=>'.');$file_type = strtr($file['type'],$type_change);
$file['name'] = $time.''.mt_rand(1,100).''.$file_type ; $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback ); // Move the file to the uploads dir

保存下,上传多媒体后文件就自动重命名为2014012315450088格式,年份月份日期时间和随机数字。

 

下面是具体的解决思路:

首先看一下一般的PHP上传照片的思路,确认类型=》多媒体重命名=》把文件上传到指定目录,成功上传后输出上传图片的预览用到的函数。

WordPress之所以不能显示中文的多媒体问题出在第二步,缺少相应的重命名。我们的思路就是在Wordpress原有的上传步骤中添加一步重命名的,这里是重命名为上传时间+随机数字。

http://www.cnblogs.com/huangcong/archive/2012/02/29/2372922.html

posted on 2014-01-23 15:45  ※WYF※  阅读(3405)  评论(0编辑  收藏  举报