wpcf7_cleanup_upload_files

The Contact Form 7 wpcf7 cleanup upload files function.

Description

wpcf7_cleanup_upload_files( (int) $seconds = 60, (int) $max = 100 ); 

Parameters (2)

0. $seconds — Optional. (int) => 60
The seconds.
1. $max — Optional. (int) => 100
The max.

Usage

  1. if ( !function_exists( 'wpcf7_cleanup_upload_files' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'contact-form-7/modules/file.php'; 
  3.  
  4. // The seconds. 
  5. $seconds = 60; 
  6.  
  7. // The max. 
  8. $max = 100; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wpcf7_cleanup_upload_files($seconds, $max); 
  12.  

Defined (1)

The function is defined in the following location(s).

/modules/file.php  
  1. function wpcf7_cleanup_upload_files( $seconds = 60, $max = 100 ) { 
  2. if ( is_admin() || 'GET' != $_SERVER['REQUEST_METHOD'] 
  3. || is_robots() || is_feed() || is_trackback() ) { 
  4. return; 
  5.  
  6. $dir = trailingslashit( wpcf7_upload_tmp_dir() ); 
  7.  
  8. if ( ! is_dir( $dir ) || ! is_readable( $dir ) || ! wp_is_writable( $dir ) ) { 
  9. return; 
  10.  
  11. $seconds = absint( $seconds ); 
  12. $max = absint( $max ); 
  13. $count = 0; 
  14.  
  15. if ( $handle = @opendir( $dir ) ) { 
  16. while ( false !== ( $file = readdir( $handle ) ) ) { 
  17. if ( $file == "." || $file == ".." || $file == ".htaccess" ) { 
  18. continue; 
  19.  
  20. $mtime = @filemtime( $dir . $file ); 
  21.  
  22. if ( $mtime && time() < $mtime + $seconds ) { // less than $seconds old 
  23. continue; 
  24.  
  25. wpcf7_rmdir_p( path_join( $dir, $file ) ); 
  26. $count += 1; 
  27.  
  28. if ( $max <= $count ) { 
  29. break; 
  30.  
  31. closedir( $handle );