2017-02-28 147 views
-3

如何通過php循環在目錄內製作目錄?
實施例:http://site_name/a/b/c/d
首先創建一個則b內的則c內然後B ....
問題是在這裏,A,B,C,D在根目錄中創建就不一一內的所有文件夾。 這裏是我的代碼 -如何通過php循環在目錄內製作目錄?

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

?> 
+1

你嘗試過這麼遠嗎?添加您的代碼 – tausun

+0

請再次檢查,我已經添加了代碼,您可以瞭解問題。 –

+1

它有什麼問題?請不要讓我們猜測,提供明確的信息,你嘗試了什麼,你的問題是什麼。 – Burki

回答

1

請檢查該代碼。它會創建嵌套文件夾,如果不退出

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

希望它會幫助你

+0

它工作正常。謝謝。 –

0

親愛的朋友們下面的答案進行了測試,在我的腳本中使用 -

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