如何按“修改日期”对此列表进行排序?

时间:2013-04-25 13:50:21

标签: php

我一直试图让这款MP3播放器通过“修改日期”来订购歌曲,但似乎无法使其正常工作。有没有办法可以通过“修改日期”输出结果?任何人都可以帮我解决这个问题吗?

    /*  Reads mp3's from a local 
directory, returns array of uri's */            
function grab_local_folder_mp3s( $folder ) {
    $items = array_multisort(

              array_map( 'filemtime', $items ),
              SORT_NUMERIC,
              SORT_DESC,
              $items
              );
    if ( ($lp = strpos($folder, $this->Rooturl)) || preg_match("!^/!", $folder) ) {
        if ( $lp !== false ) {
            $fp = str_replace($this->Rooturl, "", $folder);
            $fp = str_replace("www.", "", $fp);
            $fp = str_replace("http://", "", $fp);
            $fp = str_replace("https://", "", $fp);
        } else {
            $fp = $folder;
        }
        $path = $_SERVER['DOCUMENT_ROOT'] . $fp;
        if ($handle = @opendir($path)) {
            $j=0;
            while (false !== ($file = readdir($handle))) {
                if ( $file != '.' && $file != '..' && filetype($path.'/'.$file) == 'file' && preg_match("!\.mp3$!i", $file) ) {
                    $items[$j++] = $file;
                }
            }
            closedir($handle);
            if ( ($c = count($items)) > 0 ) {
                asort($items);

                if ( $this->folder_order != "asc" ) {
                    $items = array_reverse($items, true);
                }
                $fp = preg_replace( "!/+$!", "", $fp );
                foreach ( $items as $i => $mp3 ) {
                    $items[$i] = "http://" . $_SERVER['HTTP_HOST'] . $fp . "/" . $mp3;
                }
            }
            $this->dbug['str'] .= "\nRead folder - Done, " . $c . "mp3(s) in folder http://" . $_SERVER['HTTP_HOST'] . $fp;
            return $items; //the tracks array
        } else {
            $this->dbug['str'] .= "\nRead folder - Couldn't open local folder, check path/permissions to http://" . $_SERVER['HTTP_HOST'] . $fp;
            return true;
        }
    } else {
        $this->dbug['str'] .= "\nRead folder - Path was remote or unreadable." . $fp;
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

除非您有特殊权力,否则$items变量仍未定义:

function grab_local_folder_mp3s( $folder ) {
    $items = array_multisort(

              array_map( 'filemtime', $items ),
              SORT_NUMERIC,
              SORT_DESC,
              $items
              );

这只是你职能的开始。也许有必要考虑如何在多个函数之间拆分代码?一个好的说法是:函数中的六行。如果它变得更长,开始考虑如何将代码移出它。尝试一下,它很有意思。

还用于开发:启用最高级别的错误报告并跟踪错误日志。 PHP告诉你这些错误,你知道在哪里看起来很快。还要保持PHP更新以获得最佳的错误消息。