To remove directories and files from your website using PHP, follow these steps:
Build a PHP file To run the code, paste it into your code editor, modify the folder or file location in which you saved it, and then access it from your browser. For example, using Google Chrome.
<?php delete_files('/home/admin/web/website.com/public_html/wp-content/cache/comet-cache/cache/'); function delete_files($target) { if(is_dir($target)){ $files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned foreach( $files as $file ) { delete_files( $file ); } rmdir( $target ); } elseif(is_file($target)) { unlink( $target ); } } ?>
Also, you can create a PHP function like this...
function xrmdir($dir) { $items = scandir($dir); foreach ($items as $item) { if ($item === '.' || $item === '..') { continue; } $path = $dir.'/'.$item; if (is_dir($path)) { xrmdir($path); } else { unlink($path); } } rmdir($dir); }
For more details about removing files from the server using PHP, check it here.
You can delete a directory and its contents using PHP by using the rmdir()
and unlink()
functions.
Here's an example code to delete a directory and all its files using PHP:
$dir = 'path/to/directory'; // delete all files in the directory foreach (glob($dir.'/*') as $file) { if (is_file($file)) { unlink($file); } } // delete the directory itself if (is_dir($dir)) { rmdir($dir); }
In this code, the glob()
function is used to get a list of all files in the specified directory. The is_file()
function is used to check if each item in the list is a file, and the unlink()
function is used to delete each file.
After deleting all the files, the rmdir()
function is used to delete the directory itself, but only if it exists and is a directory.
Note that deleting files and directories using PHP can be dangerous, so be careful and make sure to use proper permissions and error handling.