dtup3446 2013-07-11 23:03
浏览 54
已采纳

如何组织从目录回显的文件?

I have this code set to display files in a directory if they're songs. They get put into a list that doesn't organize in any order. I want to organize these songs by their file name. If I look into my FileZilla they files are organized by the name of the file just fine.

example:

  • 01 - Song Title
  • 02 - Song Title
  • 03 - Song Title

The songs aren't saved in any database. I've tried putting the songs into an array and using sort() but that only outputs the word "Array" over and over. I may be doing it wrong.

you can see an example here how they get posted randomly: http://mixtapemonkey.com/mixtape?m=637

<?php
    $mixtapeid = $_GET['m'];
    $tb_name="mixtapes";

    $data = mysql_query("SELECT * FROM $tb_name WHERE id='$mixtapeid'") or die(mysql_error());
    $info = mysql_fetch_array($data);

    if ($handle = opendir("mixtapes/zip/".$info['id'])) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "mixtapes/zip/".$info['id'] && $entry != "mixtapes/zip/".$info['id']) {   
                $item = "mixtapes/zip/".$info['id']."/".$entry."/";

                    if ($newhandle = opendir("mixtapes/zip/".$info['id']."/".$entry)) {
                        while (false !== ($newentry = readdir($newhandle))) {
                            if ($newentry != "mixtapes/zip/".$info['id']."/".$entry && $newentry != "mixtapes/zip/".$info['id']."/".$entry) {

                               $ext = substr($newentry, strrpos($newentry, '.') + 1);
                               if ($ext == "mp3" || $ext == "m4a")
                               {
                                    echo    "<li><a href='$item$newentry'>".basename($newentry, '.mp3')."</a></li>";
                               }
                            }
                        }
                        closedir($newhandle);
                    }

            }
        }
        closedir($handle);
    }
?>
  • 写回答

1条回答 默认 最新

  • douyin2435 2013-07-11 23:10
    关注

    You probably want to add the songs to an array and then call the sort() (http://php.net/manual/en/function.sort.php) function.

    change

    if ($ext == "mp3" || $ext == "m4a")
    {
        echo    "<li><a href='$item$newentry'>".basename($newentry, '.mp3')."</a></li>";
    }
    

    to

    if ($ext == "mp3" || $ext == "m4a")
    {
        $musicarray[] = basename($newentry, '.mp3');
    }
    

    then later in your code, you can sort this mucis array

    sort($musicarray);

    if you get an error about the array not being defined, you might need to add

    $musicarray = Array();

    somewhere earlier in your code.

    This will only create the array and not display it. To print the array, after your giant loop you will need to echo out evrey key from the array, something like

    foreach ($musicarray as $item)
        echo $item;
    

    or equivalent

    This is untested so it might need some fiddling to get working just right.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?