简体   繁体   中英

PHP Directories

Can someone please xplain this code. Im reading larry ullmans book on php and i dont get this part. Thanks in advance!!

$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>';

它显示了当前目录中所有目录的列表

I'll repost your code with comments to exaplain what each line does.

$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>';

In short, it prints out an output of the directories inside the directory the script is running in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM