我引用了this link的来源。我的问题是我删除了删除斑马文件夹时不想删除的文件(tiger.jpg)。这是我的directories tree diagram。下面的代码:
Testing.php
include('dbconnect.php');
$sql="DELETE FROM animal WHERE animal_id='".$_POST['delete_data']."'";
if(mysqli_query($conn, $sql))
{
$sql="SELECT * FROM animal WHERE animal_id='".$_POST['delete_data']."'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
$animal_name=$row['animal_name'];
function deleteAll($str) {
//If it's a file.
if (is_file($str)) {
//Attempt to delete it.
return unlink($str);
}
//If it's a directory.
elseif (is_dir($str)) {
//Get a list of the files in this directory.
$scan = glob(rtrim($str,'/').'/*');
//Loop through the list of files.
foreach($scan as $index=>$path) {
//Call our recursive function.
deleteAll($path);
}
//Remove the directory itself.
return @rmdir($str);
}
}
//call our function
deleteAll("zoo/animal/". $animal_name);
}
我希望只删除zebra文件夹及其子文件夹和文件,而不会影响任何其他动物文件夹和文件。