The idea and realization of php new file automatic numbering

  • 2020-05-09 18:15:02
  • OfStack

Requirements: in the system in the new file is able to achieve automatic numbering. For example, the default file name of the new text file is: new text document.txt, if the new file name is changed automatically: new text document (2).txt, then 3,4,5... . How to implement this algorithm with PHP?
Ideas, originally wanted to use loop to do, think later, with increasing counter, simple and efficient, the TME can be a database, you can file, can be a configuration file, see what do you do, circulation is used at the time of maintenance, if each built a new file to do the loop one, the dead tired, cache everywhere
 
<?php 
$dir="/web/csp/images/test/"; 
if(!file_exists($dir.'cache.txt')){ 
file_put_contents($dir.'cache.txt',1); 
file_put_contents($dir.' The new file .txt',''); 
}else{ 
$num = file_get_contents($dir.'cache.txt'); 
$num ++ ; 
$name = ' The new file  ('.$num.').txt'; 
file_put_contents($dir.'cache.txt',$num); 
file_put_contents($dir.$name,''); 
}?> 

Silver children's shoes after the rewrite
 
<?php 
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, cyclic
 
<?php 

$files = scandir('.'); // This code in web So let's do it in the root  
$num = 0; 
$str = ' new   A 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 ' Successfully create file: ' . $filename; 
} 
?> 

Here is the response:
1. About the code in paragraph 1.
After automatically creating several files,
For example: the new file has been created
New file.txt
New file (2).txt
New file (3).txt
These 3 files, if deleted at this time
New file (2).txt
New file (3).txt
These two, and then execute the PHP, because of the Cache.txt counting problem, and then execute the new file is
New file (4).txt
There is no intelligent creation based on sequence.
For the above operation, the result under Windows should be the new file name
New file (2).txt

2. With regard to paragraph 2.
First of all, there must be the same problem as above, and worse still, the file created with the filename and the.delimiter of the extension missing..
That is:
test.txt
test(2)txt
test(3)txt
test(4)txt
The reason is that when combining file names, the extension points are not added.
 
file_put_contents($fileinfo['filename'] . '(' . $num . ')' .$fileinfo['extension'], $content); 


A funnier one, a shorter one.
It should be much more efficient than using caching (tmp files) or regex (preg_match) above.

<?php 
$prefix = ' new   A text document '; 
$suffix = '.txt'; 
$t = $prefix.$suffix;// new   A text document .txt 
$i = 1; 
while(file_exists($t)){// new   A text document (\d+).txt 
$t = $prefix.'('.++$i.')'.$suffix; 
} 
fclose(fopen($t, 'w')); 
?> 

Related articles: