sort alphabeticly by filename -how?

Locked
Phatline
User
Posts: 3
Joined: Sat Apr 22, 2017 1:19 am
Are you a spambot?: no

sort alphabeticly by filename -how?

Post by Phatline » Sat Apr 22, 2017 1:41 am

hello,

when you look @ my website:
http://www.example.com/musik.php
you may notice that the filenames are not sorted alphabeticly (i used demo.simple as template)

but when you look @ demo.browser:
http://www.example.com/tool/getID3/demo ... gu%2Faudio
it is sorted alphabeticly

i am not a expert, and the browser is a big project - i dont unerstand much of it, i found in a other demo that there is function to sort it by genre... but maybe it is so simple that every php beginner understand how to manage it, in theore i think - i have to write the tag data into a other array and then sort it anyhow, but that is already to much for me...:
lets say it: i am a 72ours no sleep php and html newbee, i learned a lot the last 3 days, and except of the alphabeticly sort, and a webplayer i am satisfied - thx to getID3!!!

but a little help would be nice, thx

thats the php code i used for it:

Code: Select all

       
            <?php //MP3-Tag-Scan and make List
            // include getID3() library (can be in a different directory if full path is specified)
            $writescriptfilename = 'demo.write.php';
            require_once('tool/getID3/getid3/getid3.php');
            
            // Initialize getID3 engine
            $getID3 = new getID3;
            $DirectoryToScan = 'audio'; // change to whatever directory you want to scan
            $dir = opendir($DirectoryToScan);
            echo '<table border="0" cellspacing="2" cellpadding="2">';
            echo '<tr><th>link</th><th>copyright</th><th>artist</th><th>time</th></tr>';
            while (($file = readdir($dir)) !== false) {
                $FullFileName = realpath($DirectoryToScan.'/'.$file);
                if ((substr($file, 0, 1) != '.') && is_file($FullFileName)) {
                    set_time_limit(30);
            
                    $ThisFileInfo = $getID3->analyze($FullFileName);
            
                    getid3_lib::CopyTagsToComments($ThisFileInfo);

// output desired information in whatever format you want
echo '<tr>';
echo "<td><a href=audio/{$ThisFileInfo['filename']} > {$ThisFileInfo['filename']}</a><br></td>";
echo '<td align="center">'.htmlentities(!empty($ThisFileInfo['comments_html']['copyright_message']) ? implode('<br>', $ThisFileInfo['comments_html']['copyright_message']) : chr(160)).'</td>';
echo '<td align="center">'.htmlentities(!empty($ThisFileInfo['comments_html']['band']) ? implode('<br>', $ThisFileInfo['comments_html']['band']) : chr(160)).'</td>';
echo '<td align="right">'.htmlentities(!empty($ThisFileInfo['playtime_string']) ? $ThisFileInfo['playtime_string']  : chr(160)).'</td>';
echo '</tr>';            
               
                }
            }
            echo '</table>';
            ?>
THANKS!

James Heinrich
getID3() v1 developer
Posts: 1435
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: sort alphabeticly by filename -how?

Post by James Heinrich » Sat Apr 22, 2017 2:17 am

PHP function readdir does not return files in any particular order. If you wanted them sorted alphabetically you have to do it yourself. One common way is to use the while (readdir) loop to read all the filenames into an array, sort the array, and then walk through the sorted array which will contain the filenames in the order you choose.

Phatline
User
Posts: 3
Joined: Sat Apr 22, 2017 1:19 am
Are you a spambot?: no

Re: sort alphabeticly by filename -how?

Post by Phatline » Sat Apr 22, 2017 4:04 am

i tried, but i am a completely newbee, even for a case that i am on the right, i cant see because i dont know the right syntax....
what i tried, rechered...

Code: Select all

            // Initialize getID3 engine
            $getID3 = new getID3;
            $DirectoryToScan = 'audio'; // change to whatever directory you want to scan
            $dir = opendir($DirectoryToScan);
            echo '<table border="0" cellspacing="2" cellpadding="2">';
            echo '<tr><th>link</th><th>copyright</th><th>artist</th><th>time</th></tr>';
            
            $SortedFileName = scandir($dir); //scandir should fill me my variable with alphabeticly sorted filenames?
   
            while (($file = readdir($dir)) !== false) { //do something unti there come nothing more...? file  is a counter?
                
                $FullFileName = realpath($DirectoryToScan.'/'.$SortedFileName=array($file); //how to walk thru? i have no clue
                
                if ((substr($file, 0, 1) != '.') && is_file($FullFileName)) {
                    set_time_limit(30);
            
                    $ThisFileInfo = $getID3->analyze($FullFileName);
            
                    getid3_lib::CopyTagsToComments($ThisFileInfo);

James Heinrich
getID3() v1 developer
Posts: 1435
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: sort alphabeticly by filename -how?

Post by James Heinrich » Sat Apr 22, 2017 4:34 am

Code: Select all

// change
// while (($file = readdir($dir)) !== false) {
//   $FullFileName = realpath($DirectoryToScan.'/'.$file);

// to:
$SortedFilenames = array();
while (($file = readdir($dir)) !== false) {
	$SortedFilenames[$file] = strtolower($file); // store the actual filename in the array key since it's unique, also store a lowercased version as the array value to enable case-insensitive sorting
}
asort($SortedFilenames); // sort based on the array value (lowercased filename) while maintaining the array keys which contain the actual case-sensitive filename
// loop through the sorted array, use the case-sensitive filename (stored in $file as before), ignore the lowercase filename
foreach ($SortedFilenames as $file => $lowercasefilename) {
    $FullFileName = realpath($DirectoryToScan.'/'.$file);
	// rest of code...

Phatline
User
Posts: 3
Joined: Sat Apr 22, 2017 1:19 am
Are you a spambot?: no

SOLVED

Post by Phatline » Sat Apr 22, 2017 5:42 pm

SOLVED
thx thx thx to James!

the mp3s filenames on my webpage are now sorted alphabeticly: http://www.example.com/musik.php
working code:

Code: Select all

            <?php ////////////////////////////////////////////////////////////////////////////////////////////////////// 
            //MP3-Tag-Scan and represent a "Excel" like Table/Matrix, by using James Heinrichs getID3()/////////////////
            
            // Initialize getID3 engine - only needed once
            $writescriptfilename = 'demo.write.php';
            require_once('tool/getID3/getid3/getid3.php');
            echo '<table border="0" cellspacing="2" cellpadding="2">'; //settings for the Table layout - how the ouptutet Data will be presented on the website
            echo '<tr><th>link</th><th>copyright</th><th>artist</th><th>time</th></tr>'; //labeling the Cells (Layout)
            
            // Initialize getID3 engine            
            $getID3 = new getID3; // init the process
            $DirectoryToScan = 'audio'; // scanned directory for example www.siglgut.at/audio/test.mp3
            $dir = opendir($DirectoryToScan);
            $SortedFilenames = array();
            
            // read all file names, sort them, and do the "getID3" things, and put it into in a table on the website...
            while (($file = readdir($dir)) !== false) {
                $SortedFilenames[$file] = strtolower($file); // store the actual filename in the array key since it's unique, also store a lowercased version as the array value to enable case-insensitive sorting
            }
            asort($SortedFilenames); // sort based on the array value (lowercased filename) while maintaining the array keys which contain the actual case-sensitive filename
            // loop through the sorted array, use the case-sensitive filename (stored in $file as before), ignore the lowercase filename
            foreach ($SortedFilenames as $file => $lowercasefilename) {
                $FullFileName = realpath($DirectoryToScan.'/'.$file);
                
                if ((substr($file, 0, 1) != '.') && is_file($FullFileName)) {
                    set_time_limit(30);
                    $ThisFileInfo = $getID3->analyze($FullFileName);            
                    getid3_lib::CopyTagsToComments($ThisFileInfo);

// output desired information in whatever format you want
echo '<tr>';
echo "<td><a href=audio/{$ThisFileInfo['filename']} > {$ThisFileInfo['filename']}</a><br></td>";
echo '<td align="center">'.htmlentities(!empty($ThisFileInfo['comments_html']['copyright_message']) ? implode('<br>', $ThisFileInfo['comments_html']['copyright_message']) : chr(160)).'</td>';
echo '<td align="center">'.htmlentities(!empty($ThisFileInfo['comments_html']['band']) ? implode('<br>', $ThisFileInfo['comments_html']['band']) : chr(160)).'</td>';
echo '<td align="right">'.htmlentities(!empty($ThisFileInfo['playtime_string']) ? $ThisFileInfo['playtime_string']  : chr(160)).'</td>';
echo '</tr>';            
               
                }
            }
            echo '</table>';
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            ?> 

Locked