search
HomeBackend DevelopmentPHP Tutorialfrename PHP flexible file naming function frename

To deal with this situation, some methods are to rename the new file with a timestamp, and some are to use the method of incrementing the serial number. In order to solve the troublesome operation, I recently wrote a frename function, which can flexibly obtain the uploaded file based on custom rules. The new name of the file is hereby shared.

Copy code The code is as follows:


function frename($file, $rule='{timestamp}', $force = true) {
/* ------- ----------------
* author: m35
* date: 2009/8/11
* Obtain the new name of the file according to the naming rules to handle operations such as uploading files. The file name already exists
* @parm1 $file -- the physical path of the file
* @parm2 $rule -- the naming rule, the default is the timestamp
* @parm3 $force -- whether to force the naming, if so, even if it is forced If the target file does not exist, it will be named. The default force is
* retrun str -- a new file path named according to $rule
* Example:
echo 'Default rule (timestamp):' . frename(__FILE__);
echo '
Serial number increment rule:' . frename(__FILE__, '{name}{n}');
echo '
Serial number increment rule for n leading 0s:' . frename( __FILE__, '{name}{5n}');
echo '
Use the delimiter _ and the sequence number incrementing rule with 3 leading 0s:' . frename(__FILE__ , '{3n}<_>{name}');
echo '
Rules for using time and date element combinations:' . frename(__FILE__, '{y}_{m}_{d} _{h}_{i}_{s}');
echo '
Rules for using abbreviated time and date element combinations:' . frename(__FILE__, '{Y}_{M}_ {D}_{H}_{i}_{s}');
echo '
Other custom combinations:' . frename(__FILE__, '{timestamp}_{name}<_> {n}');
----------------------- */
if (!$force && !file_exists($file)) return $file;
$filename = basename($file);
$path = str_replace($filename, '', $file);
$suffix = substr($filename, strrpos($filename, '.'));
$name = str_replace($suffix, '', $filename);
$timestamp = time();
list($y, $Y, $m, $M, $d, $D, $h, $H, $i, $s) = explode(',', date('Y,y,m,n,d,j,h,G,i,s'));
$tempname = str_replace(
array('{name}' , '{timestamp}', '{y}', '{Y}', '{m}', '{M}', '{d}', '{D}', '{h}', ' {H}', '{i}', '{s}'),
array($name, $timestamp, $y, $Y, $m, $M, $d, $D, $h, $H , $i, $s),
$rule
);
if (preg_match('/{(d?)n}/', $rule, $n)) {
preg_match('//', $tempname, $sep);
$file = $path . str_replace(array($n[0], $sep[0]), array('', ''), $ tempname) . $suffix;
if (!file_exists($file)) return $file;
$tempname = str_replace($sep[0], $sep[1], $tempname);
$tname = $tempname;
$i = 1;
do {
$nn = sprintf("%0{$n[1]}s", $i);
$tempname = str_replace($n[0], $nn, $tname);
$file = $path . $tempname . $suffix;
}
while (file_exists($file));
return $file;
} else {
$file = $path . $tempname . $suffix;
if ( file_exists($file)) return false;
else return $path . $tempname . $suffix;
}
}
echo 'Default rule (timestamp):' . frename(__FILE__);
echo '
echo '
Serial number increment rule for n leading 0s:' . frename(__FILE__, '{name}{ 5n}');
echo '
Use the delimiter _ and the sequence number incrementing rule with 3 leading 0s:' . frename(__FILE__, '{3n}{name}');
echo '
Rules for using time and date element combinations:' . frename(__FILE__, '{y}_{m}_{d}_{h}_{i }_{s}');
echo '
Rules for using abbreviated time and date element combinations:' . frename(__FILE__, '{Y}_{M}_{D}_{H} _{i}_{s}');
echo '
Other custom combinations:' . frename(__FILE__, '{timestamp}_{name}<_>{n}');
?>

The above introduces the frename PHP flexible file naming function frename, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
鸿蒙原生应用随机诗词鸿蒙原生应用随机诗词Feb 19, 2024 pm 01:36 PM

想了解更多关于开源的内容,请访问:51CTO鸿蒙开发者社区https://ost.51cto.com运行环境DAYU200:4.0.10.16SDK:4.0.10.15IDE:4.0.600一、创建应用点击File->newFile->CreateProgect。选择模版:【OpenHarmony】EmptyAbility:填写项目名,shici,应用包名com.nut.shici,应用存储位置XXX(不要有中文,特殊字符,空格)。CompileSDK10,Model:Stage。Device

使用java的File.length()函数获取文件的大小使用java的File.length()函数获取文件的大小Jul 24, 2023 am 08:36 AM

使用Java的File.length()函数获取文件的大小文件大小是在处理文件操作时很常见的一个需求,Java提供了一个很方便的方法来获取文件的大小,即使用File类的length()方法。本文将介绍如何使用该方法来获取文件的大小,并给出相应的代码示例。首先,我们需要创建一个File对象来表示我们想要获取大小的文件。以下是创建File对象的方法:Filef

php blob怎么转filephp blob怎么转fileMar 16, 2023 am 10:47 AM

php blob转file的方法:1、创建一个php示例文件;2、通过“function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })}”方法实现Blob转File即可。

使用java的File.renameTo()函数重命名文件使用java的File.renameTo()函数重命名文件Jul 25, 2023 pm 03:45 PM

使用Java的File.renameTo()函数重命名文件在Java编程中,我们经常需要对文件进行重命名的操作。Java提供了File类来处理文件操作,其中的renameTo()函数可以方便地重命名文件。本文将介绍如何使用Java的File.renameTo()函数来重命名文件,并提供相应的代码示例。File.renameTo()函数是File类的一个方法,

修复:截图工具在 Windows 11 中不起作用修复:截图工具在 Windows 11 中不起作用Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

使用java的File.getParentFile()函数获取文件的父目录使用java的File.getParentFile()函数获取文件的父目录Jul 27, 2023 am 11:45 AM

使用java的File.getParentFile()函数获取文件的父目录在Java编程中,我们经常需要操作文件和文件夹。当我们需要获取文件的父目录时,可以使用Java提供的File.getParentFile()函数来完成。本文将介绍如何使用这个函数并提供代码示例。Java中的File类是用于操作文件和文件夹的主要类。它提供了许多方法来获取和操作文件的属性

五个精选的Go语言开源项目,带你探索技术世界五个精选的Go语言开源项目,带你探索技术世界Jan 30, 2024 am 09:08 AM

在当今科技快速发展的时代,编程语言也如雨后春笋般涌现出来。其中一门备受瞩目的语言就是Go语言,它以其简洁、高效、并发安全等特性受到了许多开发者的喜爱。Go语言以其强大的生态系统而著称,其中有许多优秀的开源项目。本文将介绍五个精选的Go语言开源项目,带领读者一起探索Go语言开源项目的世界。KubernetesKubernetes是一个开源的容器编排引擎,用于自

Go语言开发必备:5个热门框架推荐Go语言开发必备:5个热门框架推荐Mar 24, 2024 pm 01:15 PM

《Go语言开发必备:5个热门框架推荐》Go语言作为一门快速、高效的编程语言,受到越来越多开发者的青睐。为了提高开发效率,优化代码结构,很多开发者选择使用框架来快速搭建应用。在Go语言的世界中,有许多优秀的框架可供选择。本文将介绍5个热门的Go语言框架,并提供具体的代码示例,帮助读者更好地理解和使用这些框架。1.GinGin是一个轻量级的Web框架,拥有快速

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.