Search code examples
phpfunctionfiledirectoryglob

Unable to get result while returning value from the function in PHP


I am unable to get the desired result with this PHP code.

I have a folder named audio in which there are 2 mp3 files as in picture Audio folder contents

And i have kept the singers name in shortform in mp3 files like RI for RIHANA and MC for MILEY CYRUS.

The thing is I want the code that scans the audio folder and checks for the playlist and finds the mp3 files according to playlist and songnumber selected. Then it will show the SINGERS NAME AVAILABLE in full form (I have made a separete function for that also) but i am unable to get SINGERS FULL NAME. It repeatedly gives the same singer name for all the different audio files that are available in folder named audio. It gives short name accurate but not the full name. You can see that in the pic. Result of PHP code when executed

So Please help me with this.

And main thing is I need THE RESULT OUTSIDE THE FUNCTION. NOT INSIDE AS I HAVE DONE.

issue


<?php
# a slightly modified version of the original function
    function singeractualname( $ssn ) {
        switch( $ssn ){
            case 'MC':return 'Miley Cyrus';
            case 'RI':return 'Rihanna';
            default:return 'Singer name not available !!!';
        }
    }
    function outputFiles( $path, $song, $dir ){
        $output=[];
        if( file_exists( realpath( $path ) ) ){
            
            $dirItr=new RecursiveDirectoryIterator( $path, RecursiveDirectoryIterator::KEY_AS_PATHNAME );
            
            foreach( new RecursiveIteratorIterator( $dirItr, RecursiveIteratorIterator::CHILD_FIRST ) as $obj => $info ) {
                if( $info->isFile() ){
                    
                    $pttn=sprintf( '@%s-\w+\.\w+@i', $song );
                    preg_match( $pttn, $info->getFileName(), $col );

                    if( !empty( $col ) ){
                        foreach( $col as $file ){
                            
                            $ext=pathinfo( $file, PATHINFO_EXTENSION );
                            list( $int, $cat, $code )=explode( '-', pathinfo( $file, PATHINFO_FILENAME ) );
                            
                            $output[]=(object)[
                                'ext'       =>  $ext,
                                'code'      =>  $code,
                                'name'      =>  singeractualname( $code ),
                                'file'      =>  $dir . '/' . $info->getFileName(),
                                'index'     =>  $int,
                                'category'  =>  $cat
                            ];                          
                        }
                    }
                }
            }
        }
        
        array_unshift( $output, (object)[
            'ext'       =>  'html',
            'code'      =>  false,
            'name'      =>  'Home',
            'file'      =>  '/homepage.html',
            'index'     =>  false,
            'category'  =>  false
        ]);
        
        return $output;
    }
    
    
    
    
    
    
    
    
    $PlaylistName = 'ROCK';
    $songNumber = 1;
    
    $root='';
    $dir='audio';
    
    $path=$root . $dir;
    $song=sprintf( '%s-%s', $songNumber, $PlaylistName );
    
    
    $arr=outputFiles( $path, $song, $dir );
    
    
    foreach( $arr as $obj ) {
        printf(
            '<link rel="stylesheet" type="text/css" href="audio/css/main.css"><div class="wrap-login100 p-l-85 p-r-85 p-t-55 p-b-55"><br><br><audio width="100%" height="100%" controls="controls" src="%1$s" controlslist="nodownload" type="audio/mp3" style="visibility: visible;">
</audio><br /><font size="4" color="#000080"><b>Singer : <font size="4" color="#B22222">%2$s</b></font></font></div>',
            $obj->file,
            $obj->name
        );
    }

Solution

  • If you want to return a list of all files found and you are hoping to recursively navigate through folders and sub-folders there needs to be, with the above code, some means of storing details for all files found which can be returned from this function. Rather than attempt to manually recurse directories and maintain a list of files found you might consider using one of the native PHP recursiveIterator type classes.

    If the format of the songs remains in a constant format you might consider a simpler method to tokenize the filename - list combined with explode allow for very easy naming of these tokens and with one of these tokens you can perform the lookup to find the full name. A regEx would be more flexible if the format were to vary between artists/directories - but the pattern below would need considerable refinement perhaps ;-)

    # a slightly modified version of the original function
        function singeractualname( $ssn ) {
            switch( $ssn ){
                case 'MC':return 'Miley Cyrus';
                case 'RI':return 'Rihanna';
                default:return 'Singer name not available !!!';
            }
        }
        function outputFiles( $path, $song, $dir ){
            $output=[];
            if( file_exists( realpath( $path ) ) ){
                
                $dirItr=new RecursiveDirectoryIterator( $path, RecursiveDirectoryIterator::KEY_AS_PATHNAME );
                
                foreach( new RecursiveIteratorIterator( $dirItr, RecursiveIteratorIterator::CHILD_FIRST ) as $obj => $info ) {
                    if( $info->isFile() ){
                        
                        $pttn=sprintf( '@%s-\w+\.\w+@i', $song );
                        preg_match( $pttn, $info->getFileName(), $col );
    
                        if( !empty( $col ) ){
                            foreach( $col as $file ){
                                
                                $ext=pathinfo( $file, PATHINFO_EXTENSION );
                                list( $int, $cat, $code )=explode( '-', pathinfo( $file, PATHINFO_FILENAME ) );
                                
                                $output[]=(object)[
                                    'ext'       =>  $ext,
                                    'code'      =>  $code,
                                    'name'      =>  singeractualname( $code ),
                                    'file'      =>  $dir . '/' . $info->getFileName(),
                                    'index'     =>  $int,
                                    'category'  =>  $cat
                                ];                          
                            }
                        }
                    }
                }
            }
            
            array_unshift( $output, (object)[
                'ext'       =>  'html',
                'code'      =>  false,
                'name'      =>  'Home',
                'file'      =>  '/homepage.html',
                'index'     =>  false,
                'category'  =>  false
            ]);
            
            return $output;
        }
        
        
        
        
        
        
        
        
        $PlaylistName = 'ROCK';
        $songNumber = 1;
        
        $root='c:/wwwroot';
        $dir='/sfx/mp3/tmp';
        
        $path=$root . $dir;
        $song=sprintf( '%s-%s', $songNumber, $PlaylistName );
        
        
        $arr=outputFiles( $path, $song, $dir );
        
        
        foreach( $arr as $obj ) {
            printf(
                '<a href="%1$s" title="%2$s">%2$s</a><br />',
                $obj->file,
                $obj->name
            );
        }
    

    Given a similar directory and content Directory and files

    The output would be

    <a href="/homepage.html" title="Home">Home</a><br />
    <a href="/sfx/mp3/tmp/1-ROCK-ACDC.mp3" title="Singer name not available !!!">Singer name not available !!!</a><br />
    <a href="/sfx/mp3/tmp/1-ROCK-MC.mp3" title="Miley Cyrus">Miley Cyrus</a><br />
    <a href="/sfx/mp3/tmp/1-ROCK-RI.mp3" title="Rihanna">Rihanna</a><br />