note 82538 deleted from control-structures.break by peehaa

From: Date: Wed, 22 Feb 2017 09:37:12 +0000
Subject: note 82538 deleted from control-structures.break by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208799@lists.php.net to get a copy of this message
Note Submitter: alan at synergymx dot com ---- Here is a function that returns specific files in an array, with all of the details. Includes some basic garbage checking. Variables $source_folder // the location of your files $ext // file extension you want to limit to (i.e.: *.txt) $sec // if you only want files that are at least so old. $limit // number of files you want to return The function function glob_files($source_folder, $ext, $sec, $limit){ if( !is_dir( $source_folder ) ) { die ( "Invalid directory.\n\n" ); } $FILES = glob($source_folder."\*.".$ext); $set_limit = 0; foreach($FILES as $key => $file) { if( $set_limit == $limit ) break; if( filemtime( $file ) > $sec ){ $FILE_LIST[$key]['path'] = substr( $file, 0, ( strrpos( $file, "\\" ) +1 ) ); $FILE_LIST[$key]['name'] = substr( $file, ( strrpos( $file, "\\" ) +1 ) ); $FILE_LIST[$key]['size'] = filesize( $file ); $FILE_LIST[$key]['date'] = date('Y-m-d G:i:s', filemtime( $file ) ); $set_limit++; } } if(!empty($FILE_LIST)){ return $FILE_LIST; } else { die( "No files found!\n\n" ); } } So.... $source_folder = "c:\temp\my_videos"; $ext = "flv"; // flash video files $sec = "7200"; // files older than 2 hours $limit = 2; // Only get 2 files print_r(glob_files($source_folder, $ext, $sec, $limit)); Would return: Array ( [0] => Array ( [path] => c:\temp\my_videos\ [name] => fluffy_bunnies.flv [size] => 21160480 [date] => 2007-10-30 16:48:05 ) [1] => Array ( [path] => c:\temp\my_videos\ [name] => synergymx.com.flv [size] => 14522744 [date] => 2007-10-25 15:34:45 )

« previous php.notes (#208799) next »