Naming files automatically in php
Get the solution ↓↓↓I was looking for a way to name my files in numbers,e.g 1.text,2.text...
But this should be done automatically. When saving A new file, the program should take the maximum number among the files in the directory, and name the new folder that number plus one.
A hint given is that we can use auto increment. How can we do so?
Answer
Solution:
$fileDir = 'your_file_dir';
$fileIterator = new FilesystemIterator($fileDir, FilesystemIterator::SKIP_DOTS);
$numberOfFiles = iterator_count($fileIterator)+1; // Count the number of files in $fileDir
$f = fopen("{$fileDir}/{$numberOfFiles}.txt", 'w');
fclose($f);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.