php delete file in folder code example
Example 1: php remove file
if(file_exists($file)) {
unlink($file);
}
Example 2: php how to delete file
if(file_exists($file)) {
unlink($file);
}
Example 3: delete file in php
unlink(filepath);
Example 4: delete file using php
unlink($Your_file_path);
if (file_exists($Your_file_path)) {
unlink($Your_file_path);
}
Example 5: php delete a folder
function deleteDirectory($dir) {
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($dir);
}