Requirement: Automatic numbering can be implemented when creating new files in the system. For example, the default file name for a new text file is: New Text Document.txt. If you continue to create a new text file, the file name automatically changes: New Text Document (2).txt. From now on, it will be 3, 4, 5... How to use this algorithm with PHP? accomplish.
The idea is that I originally wanted to use a loop to do it, but then I thought about it and used a counter to increment. It is simple and efficient. This TME can be a database, a file, or a configuration file. It depends on how you do it. The loop is only used during maintenance. , if you have to loop once every time you create a new file, it will be exhausting, and the cache is everywhere
Copy the code The code is as follows:
$dir="/web/csp/images/test/";
if (!file_exists($dir.'cache.txt')){
file_put_contents($dir.'cache.txt',1);
file_put_contents($dir.'New file.txt','');
}else {
$num = file_get_contents($dir.'cache.txt');
$num ++ ;
$name = 'New file('.$num.').txt';
file_put_contents($dir.'cache .txt',$num);
file_put_contents($dir.$name,'');
}?>
Silver Children’s Shoes’ rewritten
copy code code is as follows:
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');
?>
The third type, the loop
Copy code The code is as follows:
$files = scandir('.'); //This code is written in the web root directory
$num = 0;
$str = 'New text document';
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 'File created successfully :' . $filename;
}
?>
The following are the replies from netizens:
1. Regarding the first piece of code.
After automatically creating several files,
For example: the current newly created files are
New file.txt
New file (2).txt
New file (3).txt
If these three files are deleted at this time
New file (2).txt
New file (3).txt
These two , and then execute that PHP. Because of the problem of Cache.txt counting, the new file when executed is
New file (4).txt
It is not intelligently created based on the sequence.
The above operation is performed under Windows As a result, the name of the newly created file should be
New File (2).txt
2. Regarding the second paragraph.
First of all, there must be the above problem, and what is more serious is that the created file, file The separator between the name and the extension is missing...
That is:
test.txt
test(2)txt
test(3)txt
test(4)txt
The reason is because when combining file names .The extension dot is not added.
Copy the code The code is as follows:
file_put_contents($fileinfo['filename'] . '(' . $num . ')' .$fileinfo['extension'], $content) ;
Let’s make a more interesting and shorter paragraph.
The efficiency should be much better than the above method of using cache (tmp file) or regular expression (preg_match).
Copy the code The code is as follows:
$prefix = 'New text document';
$suffix = '.txt';
$t = $prefix.$suffix;//New text document.txt
$i = 1;
while(file_exists($t)){/ /New text document (d+).txt
$t = $prefix.'('.++$i.')'.$suffix;
}
fclose(fopen($t, 'w'));
? >