1
<?php
   mkdir("testing", 0700);
?>

I use this code can create the "testing" folder, but I hope that when I run this code to create folder each time ,it can auto add number at Folder name at first. such as

"1testing, 2testing, 3testing, 4testing.........."  

how to do it???

3 Answers 3

4

Hope this code will be helpful to you.

At first go might this solutions seems inefficient but it will work for sure without having database involvement.

Note: It will keep on checking for folder names till a specific name is available

<?php
ini_set('display_errors', 1);
$counter=1;
$path="/path/to/folder/test";
while(is_dir($path))
{
    $path="/path/to/folder/{$counter}test";
    $counter++;
}
mkdir($path);

This will create directory only when you have appropriate permissions, else it will end with.

Warning: mkdir(): Permission denied

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

Comments

0

One way you can do this is by storing a last folder no in a database table and then get that value and use it like this mkdir(i+"testing");

Comments

0
$path_to_root_folder = "/";
$tmp_list = preg_filter('/^/', $path_to_root_folder, array_diff(scandir($path_to_root_folder), array('..', '.')));

$highest = 0;
$folder_string = "testing";
foreach($tmp_list as $is_folder){
    if(is_dir($is_folder)){
        if(preg_match("#(\d+)testing#", $is_folder, $matched) && $matched[1] > $highest)
            $highest = $matched[1];
    }
}
mkdir($path_to_root_folder.($highest+1).$folder_string);

This will also work if there are other folders than your testing folders in the directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.