<?php
$dir = "/tmp";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}sort($files);print_r($files);rsort($files);print_r($files);?>

解决方案 »

  1.   

    <?php
    function getFile($dir) {
    $fileArr = array();
    $dp = opendir($dir);
    while (($file = readdir($dp)) !== false) {
    if ($file !="." && $file !=".." && $file !="") {
    if (is_dir($dir."/".$file)) {
    $fileArr = array_merge($fileArr, getFile($dir."/".$file));
    } elseif (is_file($dir."/".$file)) {
    $fileArr[] = $dir."/".$file;
    }
    }
    }
    closedir($dp);
    return $fileArr;
    }
    foreach (getFile(".") as $file) {
    echo $file."<br/>";
    }
    ?>转自
    http://community.csdn.net/Expert/topic/4761/4761228.xml?temp=.9524652
      

  2.   

    php5可以用scandir
    php4用上面的
      

  3.   

    我的一段代码,贴出来给你:
    //prepare for photo save
    $files = array();
    $file_location = $DOC_ROOT."/uploads/gmail/";
    $arr_file = scandir($file_location);
    $img = array('jpeg','jpg','bmp','gif','png','tiff','pcx');
    foreach ($arr_file as $file){
    $ext = pathinfo($file);
        if (in_array(strtolower($ext['extension']),$img)){
       $files[] = $file;
        }
    }