如何通过php循环在目录内创建目录? [英] How to make directory within directory by php loop?

查看:72
本文介绍了如何通过php循环在目录内创建目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过php循环在目录内创建目录?
示例: http://site_name/a/b/c/d
首先在a内创建一个b,然后在b内创建c,然后....
问题出在这里,a,b,c,d在根目录中创建的所有文件夹都不在一个目录中. 这是我的代码-

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,b,c,d all the folders created in root directory not one within one. Here is my code -

<?php
 $url = "http://site_name/a/b/c/d";

 $details1 = parse_url(dirname($url));

 $base_url = $details1['scheme'] . "//" . $details1['host'] . "/";

 if ($details1['host'] == 'localhost') {
    $path_init = 2;
 }else {
    $path_init = 1;
 }

 $paths = explode("/", $details1['path']);

 for ($i = $path_init; $i < count($paths); $i++) {

   $new_dir = '';
   $base_url = $base_url . $paths[$i] . "/";
   $new_dir = $base_url;
    if (FALSE === ($new_dir = folder_exist($paths[$i]))) {
      umask(0777);
       mkdir($new_dir . $paths[$i], 0777, TRUE);

  }
 }
function folder_exist($folder)
{
  // Get canonicalized absolute pathname
    $path = realpath($folder);

  // If it exist, check if it's a directory
  return ($path !== false AND is_dir($path)) ? $path : false;
}

 ?>

推荐答案

请检查此代码.如果不退出,它将创建嵌套文件夹

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);
   }
  }

希望它将对您有帮助

这篇关于如何通过php循环在目录内创建目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆