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

王朝学院·作者佚名  2016-08-22
窄屏简体版  字體: 小  |  中  |  大  |  超大  

很多用WordPRess建博的朋友都会发现,wp在上传的文件里默认是不会改变文件的原名称的,这样就导致我们有时后直接上传文件时,可能会导致中文文件名的文件出现乱码或其它问题,如果附件保存在同一个目录,也可能导致文件名重复而被覆盖。那么怎么实现wordpress上传文件自动重命名呢?下面就来给你说说。

操作方法:

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

// 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("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”;

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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航