Home >Backend Development >PHP Tutorial >Ideas and Implementation of Automatic Numbering of New Files in PHP_PHP Tutorial

Ideas and Implementation of Automatic Numbering of New Files in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:27:15950browse

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 do you use this algorithm with PHP? accomplish.
The idea was that I originally wanted to use a loop to do it. 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 for maintenance. If you have to loop it every time you create a new file, it will be exhausting. 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,'');
}?>

The rewritten version of Silver Children's Shoes
Copy code The 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, circular
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 netizens’ comments Reply:
1. Regarding the first piece of code.
After automatically creating several files,
For example: the current newly created files include
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
is not intelligently created according to the sequence.
As for the above operation, the name of the newly created file under Windows should be
New File (2).txt

2. About the second paragraph.
First of all, the above must also exist The problem, and what’s more serious is that the created file, the separator between the file name and extension is missing...
that is:
test.txt
test(2)txt
test(3)txt
test(4)txt
The reason is that when combining the file names, the dot of the extension is not added.
Copy 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 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)){// Create a new text document (d+).txt
$t = $prefix.'('.++$i.')'.$suffix;
}
fclose(fopen($t, 'w' ));
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323770.htmlTechArticleRequirement: Automatic numbering can be achieved when creating new files in the system. For example, when creating a new text file, the default file name is: New Text Document.txt. If you continue to create a new text file, the file name automatically changes:...
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