List and Sort Files with PHP DirectoryIterator

前端 未结 2 1074
遇见更好的自我
遇见更好的自我 2020-12-19 04:31

I\'m using DirectoryIterator to generate a linked-list of PDF files. (There\'s also some code to break up the file name to make the list more user friendly.) I\'d like to so

2条回答
  •  不思量自难忘°
    2020-12-19 05:17

    Use scandir instead of DirectoryIterator to get a sorted list of files:

    $path = "../images/";
    
    foreach (scandir($path) as $file){
    
        if($file[0] == '.'){continue;}//hidden file
    
        if( is_file($path.$file) ){
    
        }else if( is_dir($path.$file) ){
    
        }
    }
    

    scandir is case sensitive. For case insensitive sorting, see this answer: How do you scan the directory case insensitive?

提交回复
热议问题