How to use PHP mkdir function recursively to skip existing directory and to create new one? -
how use php mkdir function recursively skip existing directory , create new 1 string $pathname
// works fine if directories don't exist mkdir($root_dir . '/demo/test/one', 0775, true); // throw error - message: mkdir(): file exists mkdir($root_dir . '/demo/test/two', 0775, true);
what solution?
your code should work is, problem happens when/if run second time per @chris85's suggestion can check if exists beforehand.
<?php // given $root_dir = __dir__; // , want have these $dirs = [ $root_dir . '/demo/test/one', $root_dir . '/demo/test/tow', $root_dir . '/demo/test/three', $root_dir . '/demo/test/four', $root_dir . '/demo/test/and/so/on', ]; // check if they're not exists , create them foreach ($dirs $dir) { if (!is_dir($dir)) { mkdir($dir, 0775, true); } }
Comments
Post a Comment