实现wordpress上传文件自动重命名

wordpress对于上传的文件默认不改变文件的原名称,有博主可能因为文件量大而不肯意逐个重命名文件,若是直接上传的话,可能会致使中文文件 名的文件出现乱码或其它问题,若是附件保存在同一个目录,也可能致使文件名重复而被覆盖。以前使用zblog、dedecms等程序时,系统都会对上传的 文件自动重命名,搜索发现能够经过修改wordpress源代码实现文件自动重命名。

操做方法: php

  在wordpress程序的wp-admin/includes/目录中找到file.php文件,并进行编辑,在327行左右找到如下代码: wordpress

// 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'] ) );

将其替换为: code

// Move the file to the uploads dir
$new_file = $uploads['path'] ."/".date("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'] ) );

PS:总体代码其实就是替换掉了”/$filename”; blog

  保存后覆盖原文件,那么上传文件就会以“年月日时分秒+千位毫秒整数”的格式重命名文件了,如“20121023122221765.jpg” get

相关文章
相关标签/搜索