我在像这样的用户文件夹中有一个名为images的文件夹:
图片
当我从数据库中删除用户时,我还要删除该给定用户的所有文件和文件夹。所有这些都在具有函数的类中,但是当我执行函数时,我的整个文件夹图像都会被删除...
我已经检查了是否选择了正确的用户,路径是否正确。当我在test.php文件上对其进行测试时,它可以很好地工作,但是在我的函数内部却崩溃了。
如果单击删除按钮,它将进入此功能:
$user = new user();
$id = $_db->mysqli->real_escape_string($_POST['id']);
$query = "SELECT * FROM users WHERE id = '" .$id."'";
$result = $_db->mysqli->query($query);
$userNumb = $result->num_rows;
$finalUserNumb = $userNumb;
if ($finalUserNumb > 0) {
$user->deleteUser($id);
}
通过给定的ID获取用户信息
public function userSelsectByID($selector, $id)
{
$query = "SELECT " .$selector. " FROM users WHERE id ='" .$id."'";
$result = $this->mysqli->query($query);
$userInfo = $result->fetch_assoc();
$itemResult = $userInfo[$selector];
return $itemResult;
}
删除文件功能
public function delete_files($target)
{
if(is_dir($target)){
$files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
foreach( $files as $file ){
$this->delete_files( $file );
echo "Deleted ".$file." succesfull...</br>";
}
rmdir( $target );
} elseif(is_file($target)) {
unlink( $target );
}
}
删除用户功能
public function deleteUser($id)
{
$user = "DELETE FROM users WHERE id = '$id'";
$userResult = $this->mysqli->query($user);
$uren = "DELETE FROM uren WHERE user_id = '$id'";
$urenResult = $this->mysqli->query($uren);
$cookieLogin = "DELETE FROM cookieLogin WHERE user_id = '$id'";
$cookieLoginResult = $this->mysqli->query($cookieLogin);
$gebruikersnaam = str_replace(' ', '_', $this->userSelsectByID('gebruikersnaam', $id));
$this->delete_files('/sites/domain.nl/www/admin/images/'.$gebruikersnaam);
}
我希望删除带有用户名的文件夹,但实际上整个图像文件夹都被删除了...我已经尝试了很多,但是没有用:(
有人可以帮助我吗?
答案 0 :(得分:0)
我认为您在星空之前错过了一个斜杠
$files = glob( $target . '/*');
让我们做一些分析。
$this->delete_files('/sites/domain.nl/www/admin/images/test');
glob('/sites/domain.nl/www/admin/images/test/*')
rmdir('/sites/domain.nl/www/admin/images/test');
并退出该函数唯一可能出错的方法是,如果您的用户名是一个空字符串。
答案 1 :(得分:0)
在deleteUser
函数中,您先删除用户,然后再从数据库中选择用户名。
我怀疑这会导致this->userSelsectByID()
返回null
,从而导致将'/sites/domain.nl/www/admin/images/'
传递给$this->delete_files()
。
您需要调整操作顺序,以确保在从数据库中删除用户名之前已检索到该用户名,并确保在将用户名和用户目录传递给$this->delete_files()
之前,该用户名和用户目录存在。
public function deleteUser($id)
{
$gebruikersnaam = str_replace(' ', '_', $this->userSelsectByID('gebruikersnaam', $id));
$userImagesPath = '/sites/domain.nl/www/admin/images/'.$gebruikersnaam;
$user = "DELETE FROM users WHERE id = '$id'";
$userResult = $this->mysqli->query($user);
$uren = "DELETE FROM uren WHERE user_id = '$id'";
$urenResult = $this->mysqli->query($uren);
$cookieLogin = "DELETE FROM cookieLogin WHERE user_id = '$id'";
$cookieLoginResult = $this->mysqli->query($cookieLogin);
if (!empty($gebruikersnaam) && is_dir($userImagesPath)) {
$this->delete_files($userImagesPath);
}
}
作为在函数内递归使用glob
的替代方法。我建议改为使用RecursiveDirectoryIterator
。
这将允许您按照应删除它们的顺序检索所有文件和目录的列表:
给出
的目录结构/home/fyrye/test/images
- user1
- 2018-12
- image1.jpg
- image2.jpg
- 2019-01
- image1.jpg
- image2.jpg
- user2
- 2019-01
- image1.jpg
- image2.jpg
- 2018-12
- image1.jpg
- image2.jpg
然后删除user1
目录及其中的所有内容。
$directory = '/home/fyrye/test/images/user1';
$ri = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS | FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO),
RecursiveIteratorIterator::CHILD_FIRST
);
/**
* @var string $path
* @var \SplFileInfo $splFileInfo
*/
foreach ($ri as $path => $splFileInfo) {
if ($splFileInfo->isDir()) {
rmdir($path);
echo 'deleted directory: ' . $path;
} elseif ($splFileInfo->isFile()) {
unlink($path);
echo 'deleted file: ' . $path;
}
}
rmdir($directory);
echo 'deleted directory: ' $directory;
结果:
deleted file: /home/fyrye/test/images/user1/2018-12/image1.jpg
deleted file: /home/fyrye/test/images/user1/2018-12/image2.jpg
deleted directory: /home/fyrye/test/images/user1/2018-12
deleted file: /home/fyrye/test/images/user1/2019-01/image1.jpg
deleted file: /home/fyrye/test/images/user1/2019-01/image2.jpg
deleted directory: /home/fyrye/test/images/user1/2019-01
deleted directory: /home/fyrye/test/images/user1