<?php

function deleteFiles($dir)
{
    // Open the directory
    if ($handle = opendir($dir)) {
        // Loop through each file in the directory
        while (false !== ($file = readdir($handle))) {
            if ($file != '.' && $file != '..') {
                $filePath = $dir.DIRECTORY_SEPARATOR.$file;
                // If it's a directory, recursively call deleteFiles
                if (is_dir($filePath)) {
                    deleteFiles($filePath);
                } else {
                    // It's a file, attempt to delete it
                    if (unlink($filePath)) {
                        echo "File '$file' deleted successfully.<br>";
                    } else {
                        echo "Error deleting file '$file'.<br>";
                    }
                }
            }
        }
        // Close the directory handle
        closedir($handle);
        // Attempt to remove the directory itself
        if (rmdir($dir)) {
            echo "Directory '$dir' deleted successfully.<br>";
        } else {
            echo "Error deleting directory '$dir'.<br>";
        }
    }
}

$folderPath = '/home/u146541135/domains/zemail.me/public_html/public/tmp/premium/attachments/';

// Ensure the folder path exists and is a directory
if (is_dir($folderPath)) {
    deleteFiles($folderPath);
} else {
    echo "Folder '$folderPath' does not exist or is not a directory.";
}
