Php idea and implementation of creating automatic file numbers

Source: Internet
Author: User
Tags tmp file

Requirement: automatic numbering can be implemented for new files 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. Copy codeThe Code is as follows: <? Php
$ 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 rewrittenCopy codeThe Code is as follows: <? 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 ');
?>

Third, cyclicalCopy codeThe Code is as follows: <? Php

$ 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.Copy codeThe 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.Copy codeThe Code is as follows: <? Php
$ 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 '));
?>

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.