php新建文件自动编号的思路与实现_PHP
要求:在系统中 在新建文件是可以实现自动编号。比如新建文本文件 默认文件名是:新建 文本文档.txt,如果继续新建时文件名自动变了:新建 文本文档 (2).txt, 以后就是 3,4,5….请问这种算法用PHP怎么实现。
思路,原来想用循环来做,后来想想,用计数器递增吧,简单高效,这个TME可以是数据库,可以文件,可以是配置文件,看你怎么做了,循环只是在维护的时候使用的,如果每建一个新文件也得循环一次,那得累死,缓存无处不在
复制代码 代码如下:
$dir="/web/csp/images/test/";
if(!file_exists($dir.'cache.txt')){
file_put_contents($dir.'cache.txt',1);
file_put_contents($dir.'新建文件.txt','');
}else{
$num = file_get_contents($dir.'cache.txt');
$num ++ ;
$name = '新建文件 ('.$num.').txt';
file_put_contents($dir.'cache.txt',$num);
file_put_contents($dir.$name,'');
}?>
银子童鞋重写后的
复制代码 代码如下:
function createFile($filename, $content = '')
{
if(file_exists($filename . '.tmp'))
{
$num = (int) file_get_contents($filename . '.tmp') + 1;
$fileinfo = pathinfo($filename);
file_put_contents($fileinfo['filename'] . '(' . $num . ').' .$fileinfo['extension'], $content);
file_put_contents($filename . '.tmp', $num);
}
else
{
file_put_contents($filename, $content);
file_put_contents($filename . '.tmp', 1);
}
}
createFile('test.txt');
?>
第三种,循环的
复制代码 代码如下:
$files = scandir('.'); //此代码在web根目录下写就
$num = 0;
$str = '新建 文本文档';
foreach ($files as $k=> $file) {
if (is_file($file) && preg_match('/(.*)\((\d+)\)\.txt/', $file, $matched)) {
$num = $matched[2]>$num ? $matched[2] : $num;
}
}
$filename = $num == 0 ? $str . '(1).txt' : $str . '(' . ($num+1) . ').txt';
if (fopen($filename, 'w')) {
echo '成功创建文件:' . $filename;
}
?>
以下是网友的回复:
1.关于第一段代码的.
在自动新建几个文件之后,
比如:现在的新建出来的文件有
新建文件.txt
新建文件 (2).txt
新建文件 (3).txt
这三个文件,如果这个时候删除了
新建文件 (2).txt
新建文件 (3).txt
这两个,然后再执行那个PHP,因为Cache.txt计数的问题,再执行的时候新建的文件是
新建文件 (4).txt
并没有智能的根据序列创建.
而上面的操作,在Windows下的结果 新建出来的文件名应该是
新建文件 (2).txt
2.关于第二段的.
首先,肯定也存在上面的问题,而更为严重的是,创建出来的文件,文件名与扩展名的 . 分隔符丢了..
即:
test.txt
test(2)txt
test(3)txt
test(4)txt
究其原因是因为,在组合文件名的时候.没有把扩展名的点给加上去.
复制代码 代码如下:
file_put_contents($fileinfo['filename'] . '(' . $num . ')' .$fileinfo['extension'], $content);
来段更好玩的,更短的.
效率应该会比上面的使用缓存(tmp文件)或者正则(preg_match)的好很多.
复制代码 代码如下:
$prefix = '新建 文本文档';
$suffix = '.txt';
$t = $prefix.$suffix;//新建 文本文档.txt
$i = 1;
while(file_exists($t)){//新建 文本文档(\d+).txt
$t = $prefix.'('.++$i.')'.$suffix;
}
fclose(fopen($t, 'w'));
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.
