一聚教程网:一个值得你收藏的教程网站

热门教程

WordPress图片中文命名乱码问题解决办法

时间:2022-06-25 19:14:44 编辑:袖梨 来源:一聚教程网

下面以wordpress 3.2.1为例,打开wp-admin/includes/file.php文件,找到第326行这段代码:

 代码如下 复制代码

// Move the file to the uploads dir
$new_file = $uploads['path'] . "/$filename";
if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )
        return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );

  将其修改为

 代码如下 复制代码

// Move the file to the uploads dir
$new_file = $uploads['path'] . "/".date_i18n("YmdHis").floor(microtime()*1000).".".$ext;
if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )
        return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );

保存,重新上传文件。这样,新上传的文件,就会自动保存为“年月日时分秒+千位毫秒整数”的新文件名,并保存到相应的年月文件夹之下


还有一种办法

把以下代码添加到主题目录functions.php 文件 里面 判断如果是中文就自动改成md5的储存名

 代码如下 复制代码

function upload_file($filename) {
$parts = explode('.', $filename);
$filename = array_shift($parts);
$extension = array_pop($parts);
foreach ( (array) $parts as $part)
$filename .= '.' . $part;
if(preg_match('/[一-?]/u', $filename)){
$filename = md5($filename);
}
$filename .= '.' . $extension;
return $filename ;
}
add_filter('sanitize_file_name', 'upload_file', 5,1);

热门栏目