Requirements: Automatic numbering can be implemented in a new file in the system. For example, create a new text file the default file name is: New text document. txt, if you continue with new file name automatically changed: New text document (2). txt, later is 3,4,5 ... how does this algorithm use PHP to achieve.
Thought, originally wanted to use the loop to do, later think, with the counter increment bar, 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, if each build a new file also have to cycle once, that is exhausted, the cache everywhere
Copy CodeThe code is as follows:
<?php
$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 Third Kind of loop
Copy CodeThe code is as follows:
<?php
$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 a reply from a netizen:
1. About the first paragraph of code.
After you have automatically created 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 the
New file (2). txt
New file (3). txt
These two, and then execute that PHP, because Cache.txt count the problem, and then execute the time the new file is
New file (4). txt
There is no intelligent basis for the sequence creation.
And the above operation, the result of the new file created under Windows should be
New file (2). txt
2. With regard to the second paragraph.
First of all, there is certainly the problem above, and more serious is the creation of the file, filename and extension. The separator is missing.
That
Test.txt
Test (2) TXT
Test (3) TXT
Test (4) TXT
The reason for this is that, in combination with the filename, the extension is not added to the point.
Copy CodeThe code is as follows:
File_put_contents ($fileinfo [' filename ']. ' ('. $num. ') '. $fileinfo [' extension '], $content);
Come to a better play, shorter.
Efficiency should be much better than using the cache (TMP file) or regular (Preg_match).
Copy CodeThe code is as follows:
<?php
$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 '));
?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.