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

最新下载

热门教程

WordPress 3.9.0上传文件自动重命名文件方法

时间:2022-06-25 18:56:32 编辑:袖梨 来源:一聚教程网

打开“wp-admin/includes/file.php”文件,找到第313行和452行这段代码:

 代码如下 复制代码
$new_file = $uploads['path'] . "/$filename";   //主要是修改这行的代码
if ( false === @ rename( $file['tmp_name'], $new_file ) ) {
if ( 0 === strpos( $uploads['basedir'], ABSPATH ) )
$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
else
$error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $error_path ) );
}

把上面代码改成如下代码

 代码如下 复制代码
$new_file = $uploads['path'] . "/".date("YmdHis").floor(microtime()*1000).".".$ext;   //主要是修改这行的代码!
if ( false === @ rename( $file['tmp_name'], $new_file ) ) {
if ( 0 === strpos( $uploads['basedir'], ABSPATH ) )
$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
else
$error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $error_path ) );
}

核心代码上图红色那一行

 代码如下 复制代码

date("YmdHis").floor(microtime()*1000).".".$ext;

当然你也可以改更详细一些,如果你每天有大量文件上传我们可以这样修改日期分类

date("Y/M/d").floor(microtime()*1000).".".$ext;

这样就生成了如

/2013/06/18/123123.gif

了哦

最后我们再把文件保存之后上传到空间就可以了哦。

热门栏目