Learning PHP. Have recursive directory loop issue

后端 未结 4 984
情歌与酒
情歌与酒 2021-01-28 18:32

So, I\'m familiar with Javascript, HTML, and Python. I have never learned PHP, and at the moment, I\'m banging my head against my desk trying to figure out what (to me) seems t

4条回答
  •  忘掉有多难
    2021-01-28 19:03

    I try your code and make simple changes and I am able to do what you want to get.

    Here is my code ( copy of your code + Modify ) :

    function listAlbums() {
    $files = array();
    if ($handle = opendir('./photos/')) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
                //echo $entry . "
    "; $files[] = $entry; } } closedir($handle); } // Sort your folder in ascending order sort($files); // Sort your folder in descending order [ code commented ] //rsort($files); // Check your photos folder has folder or not if( !empty( $files ) ) { // Show Your Folders foreach ($files as $key => $folderName ) { echo $folderName . "
    "; } } else { echo 'You have no folder yet in photos directory'; }

    }

    My Changes:

    1. First store your all folders in the photos directory in an array variable
    2. Secondly sort this array whatever order you want.
    3. Finally show your folders (And your work will be solved)

    You can know more about this from sort-and-display-directory-list-alphabetically-using-opendir-in-php

    Thanks

提交回复
热议问题