Automatic file Rename implementation when PHP creates files

Source: Internet
Author: User
Tags php tutorial

PHP tutorial creating files with automatic file Rename implementation

<?php
function CreateFile ($filename, $content = ')
{
$fp = File_put_contents ($filename, $content);
}

The name of the file to create
$filename = ' www.txt ';

if (file_exists ($filename))
{
Open current Directory
$handle = Opendir ('./');
$fileinfo = PathInfo ($filename);
Print_r ($fileinfo);
$files = Array ();
while (false!== ($file = Readdir ($handle)))
{
if (Preg_match_all ("/{$fileinfo [' filename ']} (d*). { $fileinfo [' extension ']}/i ', $file, $match)]
{
$max = Max ($match [1]);
if ($max)
{
$createfile = $fileinfo [' filename ']. ($max + 1). '.' . $fileinfo [' extension '];
}
Else
{
$createfile = $fileinfo [' filename ']. ' 1. '. $fileinfo [' extension '];
}
}
}
CreateFile ($createfile);
}
Else
{
CreateFile ($filename);
}
?>

Use of temporary files as registers method two

<?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 ');
?

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.