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

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -