实现wordpress上传文件自动重命名
作者:xlnxin发布时间:2021-04-15分类:WordPress教程浏览:824
导读:wordpress对于上传的文件默认不改变文件的原名称,有博主可能由于文件量大而不愿意逐个重命名文件,如果直接上传的话,可能会导致中文文件名的文件出现乱码或其它问题,如果...
wordpress对于上传的文件默认不改变文件的原名称,有博主可能由于文件量大而不愿意逐个重命名文件,如果直接上传的话,可能会导致中文文件名的文件出现乱码或其它问题,如果附件保存在同一个目录,也可能导致文件名重复而被覆盖。之前使用zblog、dedecms等程序时,系统都会对上传的文件自动重命名,搜索发现可以通过修改wordpress源代码实现文件自动重命名。
操作方法:
在wordpress程序的wp-admin/includes/目录中找到file.php文件,并进行编辑,在327行左右找到以下代码:
1 2 3 4 | // 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'] ) ); |
将其替换为
1 2 3 4 | // 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”
相关推荐
- 微信小程序animation底部弹窗动画css代码
- 微信小程序自定义底部、顶部、中间、左边及右边弹窗
- 小程序component使用app.wxss
- 微信小程序引用 vant weapp calendar选择器
- 微信小程序表单各种组件源代码
- 小程序img_sec_check图片检测出行"errcode":47001错误解决方法
- 双击excel打开不显示任何内容,只能在excel里面打开文件解决方法
- 小程序saveVideoToPhotosAlbum安卓手机可以下载MP4苹果手机不能下载解决方法
- 小程序逆向错误之 typeof3 is not a function
- 小程序e.target.dataset和e.currentTarget.dataset区别
- WordPress教程排行
- 最近发表