Requirements: In the system in the new file is can be implemented automatically numbering. For example, the default file name for a new text file is: New text document. txt, if the file name changes automatically when you continue to create new text document (2). txt, which is 3,4,5 .... How can this algorithm be implemented in PHP?
Ideas, the original want to use the loop to do, and later think, with the counter increment it, simple and efficient, this tme can be a database, you can file, can be a configuration file, see how you do, the cycle is only used in the maintenance of the time, if each build a new file also have to cycle, it is exhausted, cache everywhere
Copy CodeThe 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 after rewriting
Copy CodeThe 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 kind, the cyclic
Copy CodeThe 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 ' Successfully created file: '. $filename;
}
?>
The following is the user's reply:
1. About the first paragraph of code.
After you automatically create several files,
For example: Now the newly created file has
New file. txt
New file (2). txt
New file (3). txt
These three files, if this time deleted
New file (2). txt
New file (3). txt
These two and then execute that PHP again because the Cache.txt counts the problem, and then when the new file is executed is
New file (4). txt
There is no intelligent creation based on the sequence.
And the above operation, under Windows results The new file name should be
New file (2). txt
2. With regard to the second paragraph.
First of all, there must also be the above problem, and more serious, the created file, filename and extension. The delimiter is missing.
That
Test.txt
Test (2) TXT
Test (3) TXT
Test (4) TXT
The reason is because, in the combination of file name, the extension is not added to the point.
Copy CodeThe code is as follows:
File_put_contents ($fileinfo [' filename ']. ' ('. $num. ') '. $fileinfo [' extension '], $content);
For a better play, shorter.
The efficiency should be much better than using the cache (TMP file) or regular (Preg_match) above.
Copy CodeThe 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 '));
?>
http://www.bkjia.com/PHPjc/323770.html www.bkjia.com true http://www.bkjia.com/PHPjc/323770.html techarticle requirements: In the system in the new file is can be implemented automatically numbering. For example, the new text file default file name is: New text document. txt, if the file name changes automatically when you continue with the new: ...