PHP: How can I grab a single file from a directory without scanning entire directory?

前端 未结 4 490
别跟我提以往
别跟我提以往 2020-12-29 08:59

I have a directory with 1.3 Million files that I need to move into a database. I just need to grab a single filename from the directory WITHOUT scanning the whole directory.

4条回答
  •  失恋的感觉
    2020-12-29 09:20

    do you want return first directory OR first file? both? use this:

    create function "pickfirst" with 2 argument (address and mode dir or file?)

    function pickfirst($address,$file) { // $file=false >> pick first dir , $file=true >> pick first file
    $h = opendir($address);
    
         while (false !== ($entry = readdir($h))) {
    
              if($entry != '.' && $entry != '..' && ( ($file==false && !is_file($address.$entry)) || ($file==true && is_file($address.$entry)) )  )
              { return $entry; break; } 
    
    } // end while
    } // end function
    

    if you want pick first directory in your address set $file to false and if you want pick first file in your address set $file to true.

    good luck :)

提交回复
热议问题