-6

This code for create new file works well:

<?php
$myfile = fopen("LMS-NUMBER.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

I want create files with names like: LMS-001.txt, LMS-002.txt, LMS-003.txt etc. or LMS-1.txt, LMS-2.txt, LMS-3.txt. I prefer this format: LMS-001

Thanks


I want create more files by one form submit. So after re-click on submit button, will be created files like:

1st click = LMS-1.txt;
2nd click = LMS-2.txt;
3rd click = LMS-3.txt;
7
  • sprintf() eg: $name = sprintf("LMS-%03d.txt", $number); Commented Aug 29, 2016 at 22:56
  • fopen('LMS-001.txt', 'w') Commented Aug 29, 2016 at 23:03
  • 1
    Can you be more specific about what you want? If you want to create a file with the name LMS-001.txt then it should be easy to see that you just replace "newfile.txt" with "LMS-001.txt" Commented Aug 29, 2016 at 23:06
  • "But when I want create files with names like: LMS-001.txt, LMS-002.txt, LMS-003.txt etc.? Filenames can be LMS-1.txt, LMS-2.txt, LMS-3.txt too." but when what? your question seems to drop off here. what exactly is your problem? Commented Aug 29, 2016 at 23:10
  • I need create more files by one form submit. So after repeat click on submit button, will be created files like: LMS-1.txt, LMS-2.txt, LMS-3.txt ... 1st click = LMS-1.txt; 2nd click = LMS-2.txt; 3rd click = LMS-3.txt; Sorry for my english Commented Aug 29, 2016 at 23:15

1 Answer 1

1

Even though as pointed out in comments your question is unclear, I think I understand what you want to do, though as stated, you should edit the question to make it more clear.

If I'm right, what you're asking is how to create new files with a numerical suffix one higher than the previous file to prevent overwrites. A simple way to do this is a for() loop that checks if the core file name + count number already exists, and keep going until you find one that doesn't exist already. Then you can store the filename with the iteration of the loop where the file doesn't exist, and finally write to a new file with that name. As an example;

<?php
    /* Here you can set the core filename (from your question, "LMS"),
    as well as the number of maximum files. */
    $coreFileName   = "LMS";
    $maxFilesNum    = 100;

    // Will store the filename for fopen()
    $filename = "";

    for($i = 0; $i < $maxFilesNum; $i++)
    {
        // File name structure, taken from question context
        $checkFileName = $coreFileName . "-" . str_pad($i, 3, 0, STR_PAD_LEFT) . ".txt";

        // If the file does not exist, store it in $filename for writing
        if(!file_exists($checkFileName))
        {
            $filename = $checkFileName;
            break;
        }
    }

    $fd = fopen($filename, "w");
    fwrite($fd, "Jane Doe"); // Change string literal to the name to write to file from either input or string literal
    fclose($fd); // Free the file descriptor

I've tested it and it works, so every time the page is refreshed a new file is created with the numerical suffix one higher than the previously created file. I've made this so it will only create up to a 100 files max, you can adjust this how you please with the $maxFilesNum variable near the top, however I do recommend setting a limit so that your file-system on your local or remote server doesn't get flooded with files.

Edit: Now includes padding for 001, 002 ... 100

Sign up to request clarification or add additional context in comments.

8 Comments

Thats it! Thank you! Is possible create file name like this:
LMS-001.txt, LMS-002.txt ... LMS-099.txt, LMS-100.txt ?
@Tlumic For this you can simply use str_pad() to pad the left with leading 0's until you reach the hundreds, so as an example you can use this on variable $i in the for() loop: str_pad($i, 3, 0, STR_PAD_LEFT), I've edited it into the answer if you want to see it in action. Also be sure to mark the question as answered by clicking the green check image beside my answer :)
The green check will be 100% yours ;) Please edit your answer, nice job!
Super, thank you again!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.