$file_path = "/path/to/file.txt"; if (file_exists($file_path)) { unlink($file_path); echo "File removed successfully!"; } else { echo "File does not exist."; }
$file_paths = array( "/path/to/file1.txt", "/path/to/file2.txt", "/path/to/file3.txt" ); foreach($file_paths as $file_path) { if (file_exists($file_path)) { unlink($file_path); echo "File removed: " . $file_path; } else { echo "File does not exist: " . $file_path; } }
$dir_path = "/path/to/directory"; $files = glob($dir_path . "/*"); foreach($files as $file){ if(is_file($file)) unlink($file); else if(is_dir($file)) rrmdir($file); } rmdir($dir_path);This example shows how to remove a directory and all its contents (subdirectories and files). It first uses the `glob()` function to get a list of all files and directories inside the specified directory. It then loops over the list and checks if each item is a file or a directory. If it is a file, it is removed using the `unlink()` function. If it is a directory, the function calls itself recursively to remove all its contents. Finally, the directory itself is removed using the `rmdir()` function. Package library: No external package or library is needed for this example.