php - how to restrict maximum number of files for a folder -
php - how to restrict maximum number of files for a folder -
actually iam dumping database file through php in export folder created. here want maintain latest 10 sql files in export folder kind of fifo way. not want on populate folder , want restrict 10 latest files. how ?
first grab files using glob
instance, determine if there 10 or more files in directory. after check mtime
(modified time) each , delete oldest one.
$file_pattern = '/path/to/directory/and/files/*'; $filenames = glob( $file_pattern ); if ( count( $filenames ) > 9 ) { // while there 10 or more files. while ( count( glob( $file_pattern ) ) > 9 ) { $oldest_file = null; // grab unix timestamp of *now*, filemtime returns unix timestamp too. $current_oldest_time = time(); foreach ( $filenames $filename ) { $filetime = filemtime( $filename ); if ( $filetime < $current_oldest_time ) { $current_oldest = $filetime; $oldest_file = $filename; } } // after foreach you'll have filename of oldest file (remove it),: unlink( $oldest_file ); // or null if went wrong (break out of loop): break; } }
php laravel
Comments
Post a Comment