在php中寻找目录列表

时间:2011-11-17 15:07:12

标签: php

我正在尝试获取目录列表脚本的分页。

这是该功能的原始代码。 此函数列出目录中的所有mp3文件和一些文件信息。

$songs持有数组信息

原始代码

function htmlSongs(&$tmpl, $path_raw, $songs, $custom_pls = false, $c_pls_arr = null, $lofi = false, $various, $dir_write, $song_ratings) {
global $z_url_base, $z_self, $z_img_play, $z_img_lofi, $z_img_down,
    $z_admin, $z_img_new, $z_img_edit, $z_self_raw, $zc, $z_db,$img_src;    
$i = 0;
$check_boxes = false;

$diff = $zc['new_time']*60*60*24;

if (($z_admin && $zc['cache']) || ($zc['playlists'] && $zc['session_pls']) || $zc['play_sel'] || $zc['cmp_sel']) {
    $check_boxes = true;
}
$tmpl = str_replace('{$songs_form_opts}', "name='songs' method='post' action='${z_self}p=$path_raw'", $tmpl);

$temp_section = tmpGetSect($tmpl, "SONGROW");
$html = "";
$rating = ($z_db && $zc['rating_files']);


foreach ($songs as $song => $opts) {
    $temp_song = $temp_section;
    $valign = ($opts['blurb']) ? "valign='top'" : "";
    $song_raw = getURLencodedPath($song);
    $song_title = formatSongTitle(basename($song));
    $temp_song = str_replace('{$row}', $i, $temp_song);
    $temp_song = str_replace('{$valign}', $valign, $temp_song);
    $t_blurb = ($dir_write) ? "${opts['blurb']} <a href='${z_self_raw}&amp;pl=".getURLencodedPath(basename($song))."&amp;l=22&amp;m=3'>$z_img_edit</a>" : $opts['blurb'];
    $temp_song = str_replace('{$song_blurb}', $t_blurb, $temp_song);
    $t_song_opts = "";
    $remote = false;

    if ($zc['fake'] && isset($opts['fake'])) {
        $fi = ($zc['mp3_info']) ? "<td colspan='4'>&nbsp;</td>" : "";
        $temp_song = tmpDelSec($temp_song,"SONGINFO", $fi);
        $fi = ($rating) ? "<td colspan='2'>&nbsp;</td>" : "";
        $temp_song = tmpDelSec($temp_song,"SONGVOTE", $fi);
        $temp_song = tmpDelSec($temp_song,"SONGRATING");
        $t_song_title = preg_replace("/\.(".$zc['fake_ext'].")$/i","",$song_title);
    } else {
        $url_play = $url_end = "";

        if ($zc['mp3_info'] || $zc['mp3_id3']) {
            if ($zc['remote'] && preg_match("/\.(".$zc['remote_ext'].")$/i", $song)) {
                $mp3 = new remoteFile($zc['mp3_dir']."/$song", $zc['mp3_info'], $zc['mp3_id3']);
                $remote = true;
            } else {
                $mp3 = new mp3($zc['mp3_dir']."/$song", $zc['mp3_info'], $zc['mp3_id3'], $zc['mp3_info_faster']);
            }
        }

        if ($check_boxes) {
            $checked = ($custom_pls && in_array($song, $c_pls_arr)) ? " checked='checked'" : "";
            $t_song_opts .= "<input type='checkbox' name='mp3s[]' value='$song_raw'$checked /> &nbsp; ";
        } else {
            $t_song_opts .= "&nbsp;";
        }

        if ($zc['download']) {
            if ($remote) {
                if (isset($mp3->download)) $t_song_opts .= "<a href='".$mp3->download."'>$z_img_down</a> &nbsp;&nbsp; ";
            } else {
                $surl = ($zc['stream_int']) ? "${z_self}l=12&amp;p=$song_raw" : "$z_url_base/".getURLencodedPath2($song);
                $t_song_opts .= "<a href='$surl&Oauth=$newssid'>$z_img_down</a> &nbsp;&nbsp; ";
            }
        }

        if ($zc['play']) {
            $url_play = "<a type=\"audio/mpeg\" title='$song_title' href='$mp3->url' >";
            $url_end = "</a>";
            if($mp3->url != ""){
                $t_song_opts .= "$url_play </a> &nbsp;";
            }else{
                $t_song_opts .= "";
            }
        }
        if ($zc['low']) {
            $tmp = "&nbsp;<a href='${z_self}l=8&amp;p=";
            if (isset($opts['lofi'])) {
                $t_song_opts .= "$tmp".preg_replace("/\.(".$zc['ext_mus'].")$/i", $zc['low_suf'].".$1", $song_raw).
                    "&amp;m=1'>$z_img_lofi</a> &nbsp;";
            } elseif($zc['resample'] && preg_match("/\.(".$zc['ext_enc'].")$/i", $song)) {
                $t_song_opts .= "$tmp$song_raw&amp;m=6'>$z_img_lofi</a> &nbsp;";
            }
        }
        if ($zc['new_highlight'] && isset($opts['mtime']) && ($now - $opts['mtime'] < $diff)) {
            if ($zc['new_img']) {
                $new_end = $z_img_new;
                $new_beg = "";
            } else {
                $new_beg = $zc['new_beg'];
                $new_end = $zc['new_end'];
            }
        } else {
            $new_beg = $new_end = "";
        }

        $t_song_title = "$new_beg";
        if ($zc['mp3_id3'] && $mp3->tag) {
            if ($various && !empty($mp3->artist)) $t_song_title .= "$mp3->artist - ";
            $t_song_title .= $mp3->title;
        } else {
            $subdir_images = "";
            $t_song_title .= "$song_title";
        }

        $t_song_titles = "$new_beg";
        if ($zc['mp3_id3'] && $mp3->tag) {
            if ($various && !empty($mp3->artist)) $t_song_title .= "$mp3->artist - ";
            $t_song_titles .= $mp3->title;
        } else {
            //$t_song_titles .= "<a href='$surl' type='audio/mpeg' title='$song_title'><img src='$img_src' style='display:none' /></a>";
            $t_song_titles .= "";
        }

        $t_song_title .= "$new_end";
        if ($zc['mp3_info']) {
            if ($mp3->info == 1) {
                $t_time = $mp3->time;
                $t_bitrate = "$mp3->bitrate kbps";
                $t_frequency = round($mp3->frequency/1000,1). " kHz";
                $t_size = sprintf("%.2f", round($mp3->filesize/1000000,2))." Mb";
            } else {
                $t_size = (!$remote) ? sprintf("%.2f", round($mp3->filesize/1000000,2))." Mb" : "&nbsp;";
                $t_time = $t_bitrate = $t_frequency = "&nbsp;";
            }
            $temp_song = str_replace('{$song_time}', $t_time, $temp_song);
            $temp_song = str_replace('{$song_bitrate}', $t_bitrate, $temp_song);
            $temp_song = str_replace('{$song_frequency}', $t_frequency, $temp_song);
            $temp_song = str_replace('{$song_size}', $t_size, $temp_song);
        } else {
            $temp_song = tmpDelSec($temp_song,"SONGINFO");
        }

        if ($rating) {
            $temp_song = str_replace('{$song_vote}', htmlVote($song), $temp_song);
            $rating = (!empty($song_ratings) && isset($song_ratings[$song])) ? htmlVoteFormat($song_ratings[$song]) : "&nbsp;";
            $temp_song = str_replace('{$song_rating}', $rating, $temp_song);
        } else {
            $temp_song = tmpDelSec($temp_song,"SONGVOTE");
            $temp_song = tmpDelSec($temp_song,"SONGRATING");
        }
    }
    $temp_song = str_replace('{$song_title}', $t_song_title, $temp_song);
    $temp_song = str_replace('{$song_titles}', $t_song_titles, $temp_song);
    $temp_song = str_replace('{$song_opts}', $t_song_opts, $temp_song);
    $temp_song = str_replace('{$pageshtml}', $t_pageshtml, $temp_song);

    $html .= $temp_song;
    $i = ++$i % 2;

}

$tmpl = tmpDelSec($tmpl,"SONGROW",$html);
$t_so_check = $t_so_sel = $t_so_pls = "";
if ($z_admin && $zc['cache']) {
    $submit = ($custom_pls) ? _ZUPDATECUSTOM : _ZCREATECUSTOM;
    $t_so_check .= "<input type='submit' value='$submit ' onClick=\"SubmitForm('songs','&amp;l=3&amp;z=3',false);\"/>";
}

if ($check_boxes) {
    $t_so_check .= "&nbsp; "._ZCHECK.": &nbsp;<a href='javascript:void 0;' onclick=\"CheckBoxes('songs',true);\">"._ZALL."</a> | ".
        "<a href='javascript:void 0;' onclick=\"CheckBoxes('songs',false);\">"._ZNONE."</a>";

    if ($zc['play_sel'] || $zc['cmp_sel']) {
        $url_beg2 = " &nbsp; <a class='htrack' type='audio/mpeg'  href=\"javascript:CheckIt('songs',";
        $t_so_sel .= _ZSELECTED.": ";
        $err = _ZERR_SEL;
        if ($zc['play'] && $zc['play_sel']) { $t_so_sel .= "$url_beg2'${z_self}l=8&amp;m=7',true,'$err');\">$z_img_play</a>"; }
        if ($lofi) { $t_so_sel .= "$url_beg2'${z_self}l=8&amp;m=7&amp;lf=true',true,'$err');\">$z_img_lofi</a>"; }
        if ($zc['download'] && $zc['cmp_sel']) { $t_so_sel .= "$url_beg2'&amp;l=8&amp;m=8',false,'$err');\">$z_img_down</a>"; }
    }
}
$title = substr($path_raw,strrpos($path_raw,"/")+1);
$t_so_pls = ($zc['playlists']) ? htmlPlaylistFormElements('songs') : "&nbsp;";
$tmpl = str_replace('{$row}',$i, $tmpl);
$tmpl = str_replace('{$so_check}',$t_so_check, $tmpl);
$tmpl = str_replace('{$so_select}',$t_so_sel, $tmpl);
$tmpl = str_replace('{$so_playlist}',$t_so_pls, $tmpl);
$tmpl = str_replace('{$movie_title}', $title, $tmpl);

以下是获取分页的一些解决方法,此功能正在运行,但它没有显示文件信息,如文件名,时间,大小

这是经过编辑的代码。

    function htmlSongs(&$tmpl, $path_raw, $songs, $custom_pls = false, $c_pls_arr = null, $lofi = false, $various, $dir_write, $song_ratings) {
    global $z_url_base, $z_self, $z_img_play, $z_img_lofi, $z_img_down,
        $z_admin, $z_img_new, $z_img_edit, $z_self_raw, $zc, $z_db,$img_src;    
    $i = 0;
    $check_boxes = false;
    $diff = $zc['new_time']*60*60*24;

    if (($z_admin && $zc['cache']) || ($zc['playlists'] && $zc['session_pls']) || $zc['play_sel'] || $zc['cmp_sel']) {
        $check_boxes = true;
    }
    $tmpl = str_replace('{$songs_form_opts}', "name='songs' method='post' action='${z_self}p=$path_raw'", $tmpl);

    $temp_section = tmpGetSect($tmpl, "SONGROW");
    $html = "";
    $rating = ($z_db && $zc['rating_files']);

$i = $stat_prev = 0;
$p = 1;
$currents = htmlspecialchars($_GET['showpage']);
$pages = array_chunk($songs, 10);
$t_pageshtml .= '<center>Page '.($currents+1).' of '.count($pages).'&nbsp;&nbsp;<br>Pages: ';
for($i=1; $i< count($pages)+1; $i++){
$t_pageshtml .= '<a href="?p='.htmlspecialchars($_GET["p"]).'&showpage='.($i-1).'">'.$i.'</a> &nbsp;';
}
$t_pageshtml .= '</center>';
$pgkey = (int)htmlspecialchars($_GET['showpage']);
$pages[$pgkey];

    foreach ($pages[$pgkey] as $song => $opts) {
        $temp_song = $temp_section;
        $valign = ($opts['blurb']) ? "valign='top'" : "";
        $song_raw = getURLencodedPath($song);
        $song_title = formatSongTitle(basename($song));
        $temp_song = str_replace('{$row}', $i, $temp_song);
        $temp_song = str_replace('{$valign}', $valign, $temp_song);
        $t_blurb = ($dir_write) ? "${opts['blurb']} <a href='${z_self_raw}&amp;pl=".getURLencodedPath(basename($song))."&amp;l=22&amp;m=3'>$z_img_edit</a>" : $opts['blurb'];
        $temp_song = str_replace('{$song_blurb}', $t_blurb, $temp_song);
        $t_song_opts = "";
        $remote = false;

        if ($zc['fake'] && isset($opts['fake'])) {
            $fi = ($zc['mp3_info']) ? "<td colspan='4'>&nbsp;</td>" : "";
            $temp_song = tmpDelSec($temp_song,"SONGINFO", $fi);
            $fi = ($rating) ? "<td colspan='2'>&nbsp;</td>" : "";
            $temp_song = tmpDelSec($temp_song,"SONGVOTE", $fi);
            $temp_song = tmpDelSec($temp_song,"SONGRATING");
            $t_song_title = preg_replace("/\.(".$zc['fake_ext'].")$/i","",$song_title);
        } else {
            $url_play = $url_end = "";

            if ($zc['mp3_info'] || $zc['mp3_id3']) {
                if ($zc['remote'] && preg_match("/\.(".$zc['remote_ext'].")$/i", $song)) {
                    $mp3 = new remoteFile($zc['mp3_dir']."/$song", $zc['mp3_info'], $zc['mp3_id3']);
                    $remote = true;
                } else {
                    $mp3 = new mp3($zc['mp3_dir']."/$song", $zc['mp3_info'], $zc['mp3_id3'], $zc['mp3_info_faster']);
                }
            }

            if ($check_boxes) {
                $checked = ($custom_pls && in_array($song, $c_pls_arr)) ? " checked='checked'" : "";
                $t_song_opts .= "<input type='checkbox' name='mp3s[]' value='$song_raw'$checked /> &nbsp; ";
            } else {
                $t_song_opts .= "&nbsp;";
            }

            if ($zc['download']) {
                if ($remote) {
                    if (isset($mp3->download)) $t_song_opts .= "<a href='".$mp3->download."'>$z_img_down</a> &nbsp;&nbsp; ";
                } else {
                    $surl = ($zc['stream_int']) ? "${z_self}l=12&amp;p=$song_raw" : "$z_url_base/".getURLencodedPath2($song);
                    $t_song_opts .= "<a href='$surl&Oauth=$newssid'>$z_img_down</a> &nbsp;&nbsp; ";
                }
            }

            if ($zc['play']) {
                $url_play = "<a type=\"audio/mpeg\" title='$song_title' href='$mp3->url' >";
                $url_end = "</a>";
                if($mp3->url != ""){
                    $t_song_opts .= "$url_play </a> &nbsp;";
                }else{
                    $t_song_opts .= "";
                }
            }
            if ($zc['low']) {
                $tmp = "&nbsp;<a href='${z_self}l=8&amp;p=";
                if (isset($opts['lofi'])) {
                    $t_song_opts .= "$tmp".preg_replace("/\.(".$zc['ext_mus'].")$/i", $zc['low_suf'].".$1", $song_raw).
                        "&amp;m=1'>$z_img_lofi</a> &nbsp;";
                } elseif($zc['resample'] && preg_match("/\.(".$zc['ext_enc'].")$/i", $song)) {
                    $t_song_opts .= "$tmp$song_raw&amp;m=6'>$z_img_lofi</a> &nbsp;";
                }
            }
            if ($zc['new_highlight'] && isset($opts['mtime']) && ($now - $opts['mtime'] < $diff)) {
                if ($zc['new_img']) {
                    $new_end = $z_img_new;
                    $new_beg = "";
                } else {
                    $new_beg = $zc['new_beg'];
                    $new_end = $zc['new_end'];
                }
            } else {
                $new_beg = $new_end = "";
            }

            $t_song_title = "$new_beg";
            if ($zc['mp3_id3'] && $mp3->tag) {
                if ($various && !empty($mp3->artist)) $t_song_title .= "$mp3->artist - ";
                $t_song_title .= $mp3->title;
            } else {
                $subdir_images = "";
                $t_song_title .= "$song_title";
            }

            $t_song_titles = "$new_beg";
            if ($zc['mp3_id3'] && $mp3->tag) {
                if ($various && !empty($mp3->artist)) $t_song_title .= "$mp3->artist - ";
                $t_song_titles .= $mp3->title;
            } else {
                //$t_song_titles .= "<a href='$surl' type='audio/mpeg' title='$song_title'><img src='$img_src' style='display:none' /></a>";
                $t_song_titles .= "";
            }

            $t_song_title .= "$new_end";
            if ($zc['mp3_info']) {
                if ($mp3->info == 1) {
                    $t_time = $mp3->time;
                    $t_bitrate = "$mp3->bitrate kbps";
                    $t_frequency = round($mp3->frequency/1000,1). " kHz";
                    $t_size = sprintf("%.2f", round($mp3->filesize/1000000,2))." Mb";
                } else {
                    $t_size = (!$remote) ? sprintf("%.2f", round($mp3->filesize/1000000,2))." Mb" : "&nbsp;";
                    $t_time = $t_bitrate = $t_frequency = "&nbsp;";
                }
                $temp_song = str_replace('{$song_time}', $t_time, $temp_song);
                $temp_song = str_replace('{$song_bitrate}', $t_bitrate, $temp_song);
                $temp_song = str_replace('{$song_frequency}', $t_frequency, $temp_song);
                $temp_song = str_replace('{$song_size}', $t_size, $temp_song);
            } else {
                $temp_song = tmpDelSec($temp_song,"SONGINFO");
            }

            if ($rating) {
                $temp_song = str_replace('{$song_vote}', htmlVote($song), $temp_song);
                $rating = (!empty($song_ratings) && isset($song_ratings[$song])) ? htmlVoteFormat($song_ratings[$song]) : "&nbsp;";
                $temp_song = str_replace('{$song_rating}', $rating, $temp_song);
            } else {
                $temp_song = tmpDelSec($temp_song,"SONGVOTE");
                $temp_song = tmpDelSec($temp_song,"SONGRATING");
            }
        }
        $temp_song = str_replace('{$song_title}', $t_song_title, $temp_song);
        $temp_song = str_replace('{$song_titles}', $t_song_titles, $temp_song);
        $temp_song = str_replace('{$song_opts}', $t_song_opts, $temp_song);
        $temp_song = str_replace('{$pageshtml}', $t_pageshtml, $temp_song);

        $html .= $temp_song;
        $i = ++$i % 2;
        $p++;
    }

    $tmpl = tmpDelSec($tmpl,"SONGROW",$html);
    //$t_pageshtml = 'UUUU';
    $t_so_check = $t_so_sel = $t_so_pls = "";
    if ($z_admin && $zc['cache']) {
        $submit = ($custom_pls) ? _ZUPDATECUSTOM : _ZCREATECUSTOM;
        $t_so_check .= "<input type='submit' value='$submit ' onClick=\"SubmitForm('songs','&amp;l=3&amp;z=3',false);\"/>";
    }

    if ($check_boxes) {
        $t_so_check .= "&nbsp; "._ZCHECK.": &nbsp;<a href='javascript:void 0;' onclick=\"CheckBoxes('songs',true);\">"._ZALL."</a> | ".
            "<a href='javascript:void 0;' onclick=\"CheckBoxes('songs',false);\">"._ZNONE."</a>";

        if ($zc['play_sel'] || $zc['cmp_sel']) {
            $url_beg2 = " &nbsp; <a class='htrack' type='audio/mpeg'  href=\"javascript:CheckIt('songs',";
            $t_so_sel .= _ZSELECTED.": ";
            $err = _ZERR_SEL;
            if ($zc['play'] && $zc['play_sel']) { $t_so_sel .= "$url_beg2'${z_self}l=8&amp;m=7',true,'$err');\">$z_img_play</a>"; }
            if ($lofi) { $t_so_sel .= "$url_beg2'${z_self}l=8&amp;m=7&amp;lf=true',true,'$err');\">$z_img_lofi</a>"; }
            if ($zc['download'] && $zc['cmp_sel']) { $t_so_sel .= "$url_beg2'&amp;l=8&amp;m=8',false,'$err');\">$z_img_down</a>"; }
        }
    }
    $title = substr($path_raw,strrpos($path_raw,"/")+1);
    $t_so_pls = ($zc['playlists']) ? htmlPlaylistFormElements('songs') : "&nbsp;";
    $tmpl = str_replace('{$row}',$i, $tmpl);
    $tmpl = str_replace('{$so_check}',$t_so_check, $tmpl);
    $tmpl = str_replace('{$so_select}',$t_so_sel, $tmpl);
    $tmpl = str_replace('{$so_playlist}',$t_so_pls, $tmpl);
    $tmpl = str_replace('{$movie_title}', $title, $tmpl);
    $tmpl = str_replace('{$pages_html}', $t_pageshtml, $tmpl);

}

这是分页的代码

$i = $stat_prev = 0;
$p = 1;
$currents = htmlspecialchars($_GET['showpage']);
$pages = array_chunk($songs, 10);
$t_pageshtml .= '<center>Page '.($currents+1).' of '.count($pages).'&nbsp;&nbsp;<br>Pages: ';
for($i=1; $i< count($pages)+1; $i++){
$t_pageshtml .= '<a href="?p='.htmlspecialchars($_GET["p"]).'&showpage='.($i-1).'">'.$i.'</a> &nbsp;';
}
$t_pageshtml .= '</center>';
$pgkey = (int)htmlspecialchars($_GET['showpage']);
$pages[$pgkey];

我认为这里有问题

原始代码

foreach ($songs as $song => $opts)

已编辑的代码

 foreach ($pages[$pgkey] as $song => $opts) {

确定

我解决了自己的问题。

这是解决方案

我使用array_slice

$i = $stat_prev = 0;
$p = 1;
$currents = htmlspecialchars($_GET['showpage']);
$pages = array_chunk($songs, 10);
$t_pageshtml .= '<center>Page '.($currents+1).' of '.count($pages).'&nbsp;&nbsp;<br>Pages: ';
for($i=1; $i< count($pages)+1; $i++){
$t_pageshtml .= '<a href="?p='.htmlspecialchars($_GET["p"]).'&showpage='.($i-1).'">'.$i.'</a> &nbsp;';
}
$t_pageshtml .= '</center>';
$pgkey = (int)htmlspecialchars($_GET['showpage']);

$songs = array_slice($songs, $pgkey."0",10);

    foreach ($songs as $song => $opts ) {

1 个答案:

答案 0 :(得分:0)

您可以使用array_chunk函数“”任何一维数组:

$pages = array_chunk($songs, $perPage = 10);
$pageCount = count($pages);
$currentPageSongs = $pages[$page-1]; # pages are zero-based