0

I have a php loop and I have the code that creates a directory...

<?php

for ($i = 0; $i < 925; $i++){


    if (!file_exists('C:\wamp\www\maindirectory\mydirectory\0001')) {
        mkdir('C:\wamp\www\maindirectory\mydirectory\0001', 0777, true);
    }
}

?>

Obviously this code will not work. However, where 0001 is in the above code, I need it to count up as 0001, 0002, 0003, etc. The last directory would be 0925. How can I do this?

4
  • Whats this line all about "; I would guess if you removed that the script might compile and run Commented Jul 16, 2018 at 19:44
  • Look into str_pad() Commented Jul 16, 2018 at 19:45
  • Or sprintf() Commented Jul 16, 2018 at 19:47
  • @RiggsFolly or even just str_repeat( '0', 4 - strlen( $i ) ).$i Commented Jul 16, 2018 at 19:50

1 Answer 1

2

You could use sprintf => http://www.php.net/manual/en/function.sprintf.php

In your case:

mkdir('C:/wamp/www/maindirectory/mydirectory/' . sprintf("%04d", $i), 0777, true);

I think you will manage to finish the code.

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

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.