2011-08-20 24 views
0

有人可以請xplain此代碼。我讀larry ullmans書上的PHP和我沒有得到這部分。提前致謝!!PHP目錄

$search_dir = '.'; 
$contents = scandir($search_dir); 

    print '<h2>Directories</h2> 
    <ul>'; 
    foreach ($contents as $item) { 
    if ((is_dir($search_dir . '/' . $item)) AND (substr($item, 0, 1) != '.')) { 
    print "<li>$item</li>\n"; 
    } 
    } 
print '</ul>'; 

回答

0

我會重新發布與評論你的代碼exaplain每一行做什麼。

$search_dir = '.'; //Set the directory to search. "." means the current one. 
$contents = scandir($search_dir); //scan the directory and return its results into $contents 

print '<h2>Directories</h2> 
    <ul>'; 
foreach($contents as $item) { //Iterate through the array and for each item... 
    //If the item is a directory  AND it doesn't start with a . (which means the current one)... 
    if ((is_dir($search_dir.'/'.$item)) AND(substr($item, 0, 1) != '.')) { 
     print "<li>$item</li>\n"; //Print it 
    } 
} 
print '</ul>'; 

總之,它打印出的目錄的輸出腳本運行的目錄中。

+0

謝謝!我有一個問題,但。如果我拿$ search_dir。'/'。離開相同的結果出現。爲什麼需要? – DividedDreams

+0

這是因爲如果你的$ search_dir不同於'.'(你想得到不同目錄下的目錄),你會失敗,因爲它會查找'is_dir($ item)'劇本)。巧合的是,搜索目錄和當前目錄是相同的。 –

+0

不好意思是一種痛苦,但$內容的數組看起來像什麼樣,$ item是怎麼樣的? – DividedDreams

1

它顯示你所有的目錄列表,在當前目錄