Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active September 15, 2016 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/05ca730f85a7ca31471017de7b4eadc4 to your computer and use it in GitHub Desktop.
Save tommcfarlin/05ca730f85a7ca31471017de7b4eadc4 to your computer and use it in GitHub Desktop.
[WordPress] Read All Files of a Certain Type / Delete All Files in a Directory
<?php
public function read_files() {
$uploads_directory = trailingslashit(
plugin_dir_path( dirname( __FILE__ ) )
) . 'uploads/';
$files = array();
foreach ( glob( $uploads_directory . 'file_*.txt' ) as $file ) {
array_push( $files, $file );
}
return $files;
}
<?php
public function delete_all_files() {
$directory = trailingslashit( $uploads_directory );
$files = glob( $directory . '{,.}*', GLOB_BRACE );
foreach ( $files as $file ) {
if ( is_file( $file ) ) {
unlink( $file );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment