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 the code code 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, the cyclic
Copy the code code 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 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 the code code 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 the code code 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.