Learning PHP. Have recursive directory loop issue

后端 未结 4 905
情歌与酒
情歌与酒 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 18:48

    Whew! Ok, I got everything working. DirectoryIterator was pissing me off with its random print order. So I fell back to just glob and scandir. Those seem to print out a list "logically". To summarize, this bit of php will grab folders of images and turn each folder into its own lightbox gallery. Each gallery shows up as a hyperlink that triggers the lightbox gallery. I'm pretty happy with it!

    So here's my final code:

    <?php
    function listAlbums(){
    
        $directory = "./photos/";
    
        //get all folders in specified directory
        $folders = glob($directory . "*");
    
        //get each folder item name
        foreach($folders as $folderItem){
            //check to see if the folderItem is a folder/directory
            if(is_dir($folderItem)){
                $count = 0;
                $scanDir = scandir($folderItem);
                foreach($scanDir as $file){
                    if ($file === '.' || $file === '..') continue;
                    $filePath = $folderItem."/".$file;
                    if ($count == 0){
                        echo '<a href="'.$filePath.'" data-lightbox="'.basename($folderItem).' "data-title="'.basename($folderItem)." ".strpbrk(basename($filePath, ".jpg"), '-').'">'.basename($folderItem).'</a>' . "<br/>";
                        echo "<div style='display: none'>" . "<br/>";
                        $count++;
                    }else{
                        echo '<a href="'.$filePath.'" data-lightbox="'.basename($folderItem).' "data-title="'.basename($folderItem)." ".strpbrk(basename($filePath, ".jpg"), '-').'">'.basename($folderItem).'</a>' . "<br/>";
                    }
                }echo "</div>";
            }
        }
    }       
    

    Special thanks to Rasclatt for bearing with me through all of this and offering up a lot of hand-holding and help. I really appreciate it!

    0 讨论(0)
  • 2021-01-28 18:54

    Try one of these, one is recursive, one is not:

    // Recursive
    function listAlbums($path = './photos/')
        {
            $objects    =   new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
            foreach($objects as $name => $object) {
                ob_start();
                echo rtrim($object,".");
                $data   =   ob_get_contents();
                ob_end_clean();
    
                $arr[] = $data;
            }
    
            return array_unique($arr);
        }
    // Non-recursive
    function getAlbums($path = './photos/')
        {
            $objects    =   dir($path);
    
            while (false !== ($entry = $objects->read())) {
                if($entry !== '.' && $entry !== '..')
                    $arr[]  =   $path.$entry;
            }
    
            $objects->close();
    
            return $arr;
        }
    
    // I would use __DIR__, but not necessary
    $arr    =   listAlbums();
    $arr2   =   getAlbums();
    
    // Reverse arrays by key
    krsort($arr);
    krsort($arr2);
    
    // Show arrays
    print_r($arr);
    print_r($arr2);
    
    0 讨论(0)
  • 2021-01-28 18:56

    Ok, this is the best result i've gotten all night. This does 99% of what I need. But I still can't figure out this damn sort issue. This code will go through a list of directories, and create lightbox galleries out of the images in each folder. However, the first image is always the last one in the array, and I can't figure out how the heck to get it to sort normally! Check line 16 to see where I talking about.

    <?php
    function listAlbums(){
        $directory = "./photos/";
    
        //get all folders in specified directory
        $folders = glob($directory . "*");
    
        //get each folder item name
        foreach($folders as $folderItem){
            //check to see if the folderItem is a folder/directory
            if(is_dir($folderItem)){
                $count = 0;
                foreach (new DirectoryIterator($folderItem) as $fileInfo) {
                    if($fileInfo->isDot()) continue;
                    $files = $folderItem."/".$fileInfo->getFilename();
                    if ($count == 0){  //This is where my issues start.  This file ends up being the last one in the list.  I need it to be the first.
                        echo '<a href="'.$files.'" data-lightbox="'.basename($folderItem).' "data-title="'.basename($files).'">'.basename($folderItem).'</a>' . "<br/>";
                        echo "<div style='display: none'>" . "<br/>";
                        $count++;
                    }else{
                        echo '<a href="'.$files.'" data-lightbox="'.basename($folderItem).' "data-title="'.basename($files).'">'.basename($folderItem).'</a>' . "<br/>";
                    }
                }echo "</div>"; 
            }
        }
    }       
    

    ?>

    0 讨论(0)
  • 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 . "<br/>";
                $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 . "<br/>";
        }
    } 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

    0 讨论(0)
提交回复
热议问题