Php idea and implementation of creating automatic file numbers. Requirement: you can create a file automatically in the system. For example, if the default file name of a new text file is "new text file .txt", the file name automatically changes when the new file is created. requirement: the file name can be automatically numbered in the system. For example, the default file name for creating a new text file is: create a text file .txt. if the file name changes automatically when the new file is continued: create a text file (2).txt, which will be 3, 4, 5 .... How can I implement this algorithm using PHP.
The idea is that we wanted to use a loop to do it. later we thought about it. it was simple and efficient to use counters. This TME can be a database, a file, or a configuration file. let's see how you did it, the loop is used only during maintenance. if a new file has to be recycled once, the cache is everywhere.
The code is as follows:
$ Dir = "/web/csp/images/test /";
If (! File_exists(nvidir.'cache.txt ')){
File_put_contents(nvidir.'cache.txt ', 1 );
File_put_contents(nvidir.'new file .txt ','');
} Else {
$ Num = file_get_contents(nvidir.'cache.txt ');
$ Num ++;
$ Name = 'new file ('.w.num.'{.txt ';
File_put_contents(nvidir.'cache.txt ', $ num );
File_put_contents ($ dir. $ name ,'');
}?>
After the silver kids shoes are rewritten
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 ');
?>
Third, cyclical
The code is as follows:
$ Files = scandir ('.'); // This code is written in the web root directory.
$ Num = 0;
$ Str = 'create 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 'file created successfully: '. $ filename;
}
?>
The following is a reply from a netizen:
1. about the first piece of code.
After several files are automatically created,
For example, the newly created files include
New file .txt
Create a file (22.16.txt
Create a file (32.16.txt
If the three files are deleted at this time
Create a file (22.16.txt
Create a file (32.16.txt
Then execute the PHP again. The new file is
Create a file (42.16.txt
There is no smart sequence creation.
In the above operation, the file name created in the result of Windows should be
Create a file (22.16.txt
2. about Section 2.
First of all, the above problem also exists, and more seriously, the. separator between the file name and the extension is lost in the created file ..
That is:
Test.txt
Test (2) txt
Test (3) txt
Test (4) txt
The reason is that when the file name is combined, the extension points are not added.
The code is as follows:
File_put_contents ($ fileinfo ['filename']. '('. $ num. ')'. $ fileinfo ['extension'], $ content );
It is more fun and shorter.
The efficiency should be much better than the previous cache (tmp file) or regular expression (preg_match.
The code is as follows:
$ Prefix = 'create a text document ';
$ Suffix = '.txt ';
$ T = $ prefix. $ suffix; // create a text file. txt
$ I = 1;
While (file_exists ($ t) {// create a text document (\ d1_0000.txt
$ T = $ prefix. '('. ++ $ I. ')'. $ suffix;
}
Fclose (fopen ($ t, 'w '));
?>
The marker can be automatically numbered when a new file is created. For example, the default file name for creating a text file is: create a text file .txt. if the file name changes automatically when the new file is created :...