How to make directory within directory by php loop?

后端 未结 3 623
萌比男神i
萌比男神i 2021-01-29 14:53

How to make directory within directory by php loop?
Example: http://site_name/a/b/c/d
First create a then b within a then c within b then ....
Problem is here a

相关标签:
3条回答
  • 2021-01-29 15:06

    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);
           }
       }
    ?>
    
    0 讨论(0)
  • 2021-01-29 15:19

    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

    0 讨论(0)
  • 2021-01-29 15:25

    You can actually create nested folders with the mkdir PHP function

    mkdir($path, 0777, true); // the true value here = recursively
    
    0 讨论(0)
提交回复
热议问题