How to make directory within directory by php loop?

眉间皱痕 提交于 2019-12-02 13:36:27

please check this code. it will create nested folder if not exit

<?php
 $your_path = "Bashar/abc/def/ghi/dfsdfds/get_dir.php";
 $array_folder = explode('/', $your_path);
 $mkyourfolder = "";

 foreach ($array_folder as $folder) {
   $mkyourfolder = $mkyourfolder . $folder . "/";
   if (!is_dir($mkyourfolder)) {
     mkdir($mkyourfolder, 0777);
   }
  }

hope it will help you

Dear friends the following answer is tested and used in my script -

<?php
   $url = "http://localhost/Bashar/abc/def/ghi/dfsdfds/get_dir.php";
   $details = parse_url(dirname($url));
   //$base_url = $details['scheme'] . "//" . $details['host'] . "/";
   $paths = explode("/", $details['path']);
   $full_dir = '';
   $init = ($details['host'] == 'localhost') ? '2' : '1';
   for ($i = $init; $i < count($paths); $i++) {
       $full_dir = $full_dir . $paths[$i] . "/";
       if (!is_dir($full_dir)) {
          mkdir($full_dir, 0777);
       }
   }
?>

You can actually create nested folders with the mkdir PHP function

mkdir($path, 0777, true); // the true value here = recursively
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!