Empty a folder on the server

Make sure you have unique name directory

$directory = “images/directory”;
empty($directory);
function empty($directory)
{
$dirHandle = opendir($directory);
// Loop over all of the files in the folder
while ($file = readdir($dirHandle)) {
// If $file is NOT a directory remove it
if(!is_dir($file)) {
unlink (“$directory”.”$file”); // unlink() deletes the files
}
}
// Close the directory
closedir($dirHandle);

// you can skip this state in case you just want to remove directory only

if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
echo “directory empty successfully!”;
}

Published by Sushant Sharma

Technologist. Software Developer. Tech Analyst ।।।

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started