note 82538 added to control-structures.break
| From: | alan at synergymx dot com | Date: | Tue, 15 Apr 2008 19:06:56 +0000 |
| Subject: | note 82538 added to control-structures.break | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138336@lists.php.net to get a copy of this message | ||
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
)
----
Server IP: 209.41.74.194
Probable Submitter: 64.60.194.194
----
Manual Page -- http://www.php.net/manual/en/control-structures.break.php
Edit -- https://master.php.net/note/edit/82538
Del: integrated -- https://master.php.net/note/delete/82538/integrated
Del: useless -- https://master.php.net/note/delete/82538/useless
Del: bad code -- https://master.php.net/note/delete/82538/bad+code
Del: spam -- https://master.php.net/note/delete/82538/spam
Del: non-english -- https://master.php.net/note/delete/82538/non-english
Del: in docs -- https://master.php.net/note/delete/82538/in+docs
Del: other reasons-- https://master.php.net/note/delete/82538
Reject -- https://master.php.net/note/reject/82538
Search -- https://master.php.net/manage/user-notes.php