Deleting all files from a folder using PHP?
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file)) {
unlink($file); // delete file
}
}
If you want to remove 'hidden' files like .htaccess, you have to use
$files = glob('path/to/temp/{,.}*', GLOB_BRACE);
PHP: Unlink All Files Within A Directory, and then Deleting That Directory
Use glob
to find all files matching a pattern.
function recursiveRemoveDirectory($directory)
{
foreach(glob("{$directory}/*") as $file)
{
if(is_dir($file)) {
recursiveRemoveDirectory($file);
} else {
unlink($file);
}
}
rmdir($directory);
}
Delete all files with a php script
you can delete all files using for loop, example:
`
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
`
Delete directory with files in it?
There are at least two options available nowadays.
Before deleting the folder, delete all its files and folders (and this means recursion!). Here is an example:
public static function deleteDir($dirPath) {
if (! is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
self::deleteDir($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}And if you are using 5.2+ you can use a RecursiveIterator to do it without implementing the recursion yourself:
$dir = 'samples' . DIRECTORY_SEPARATOR . 'sampledirtree';
$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it,
RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($dir);
Remove all files, folders and their subfolders with php
<?php
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir")
rrmdir($dir."/".$object);
else unlink ($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
?>
Try out the above code from php.net
Worked fine for me
php function to delete all files & folders older than 1 day
The difficulty is that it depends on the intention of the caller. Should they pass in a directory and if so - should it remove this directory. As the routine is called rrmdir
I would sort of expect it to remove the directory I was passing in.
function rrmdir($dir)
{
echo $dir.PHP_EOL;
if (is_dir($dir))
{
$objects = scandir($dir);
foreach ($objects as $object)
{
if ($object != "." && $object != "..")
{
echo $object.'='.filetype($dir."/".$object).PHP_EOL;
if (filetype($dir."/".$object) == "dir") {
rrmdir($dir."/".$object);
rmdir($dir);
}
if (filemtime($dir."/".$object) <= time()-60*60*24) unlink($dir."/".$object);
}
}
reset($objects);
//rmdir($dir);
}
}
I've moved the rmdir
into the test for sub-directories. It was previously removing all directories (including the one passed in).
Related Topics
Exception: Serialization of 'Closure' Is Not Allowed
How to Get Xdebug Var_Dump to Show Full Object/Array
How to Write a Stored Procedure Using PHPmyadmin and How to Use It Through PHP
How to Run PHP Files on My Computer
How to Add Text to an Image with PHP Gd Library
How to Check If a Url Exists in PHP
Phpmyadmin - Count(): Parameter Must Be an Array or an Object That Implements Countable
JSON_Encode Function: Special Characters
Deleting All Files from a Folder Using PHP
Yosemite/El Capitan PHP-Gd + Mcrypt Installation