我能';在传递连接字符串时,是否在PHP中删除目录?
我在PHP程序中使用deleteFiles()函数来删除目录。它仅在传入显式定义的字符串时有效,但在传入与变量连接的字符串时无效 这不起作用:我能';在传递连接字符串时,是否在PHP中删除目录?,php,windows,web,directory,Php,Windows,Web,Directory,我在PHP程序中使用deleteFiles()函数来删除目录。它仅在传入显式定义的字符串时有效,但在传入与变量连接的字符串时无效 这不起作用: // Checks if the user hit the delete button if(isset($_POST['delete'])){ $targetDir = "uploads/$id/"; ### This does not work when I pass it in deleteFiles($targetDir);
// Checks if the user hit the delete button
if(isset($_POST['delete'])){
$targetDir = "uploads/$id/"; ### This does not work when I pass it in
deleteFiles($targetDir);
// Get the id to delete
$idToDelete = mysqli_real_escape_string($conn, $_POST['idToDelete']);
// Create sql
$sql = "DELETE FROM student_data WHERE id = $idToDelete";
// Redirects user to the homepage if the query is successful
if(mysqli_query($conn, $sql)){
header('Location: index.php');
} else {
echo 'query error: '. mysqli_error($conn);
}
}
function deleteFiles($target) {
$files = glob($target . "/*"); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file)) {
unlink($file); // delete file
}
}
rmdir($target);
}
这确实有效:
// Checks if the user hit the delete button
if(isset($_POST['delete'])){
$targetDir = "uploads/82/"; ### If I pass in this it works
deleteFiles($targetDir);
// Get the id to delete
$idToDelete = mysqli_real_escape_string($conn, $_POST['idToDelete']);
// Create sql
$sql = "DELETE FROM student_data WHERE id = $idToDelete";
// Redirects user to the homepage if the query is successful
if(mysqli_query($conn, $sql)){
header('Location: index.php');
} else {
echo 'query error: '. mysqli_error($conn);
}
}
function deleteFiles($target) {
$files = glob($target . "/*"); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file)) {
unlink($file); // delete file
}
}
rmdir($target);
}
我尝试使用===运算符比较这两个字符串,结果返回1。不知道为什么会发生这种情况。实际上,
$targetDir
末尾有一个斜杠,这意味着它不是目录或“文件夹”,而是指向某些文件和文件夹的路径。“uploads/$id/”
。删除尾随斜杠,它应该可以工作。。。。请不要让任何人就这个确切的解决方案给出答案。但我离题了。如果我回应它,我会得到“uploads/82/”。我尝试了@GetSet solution。旁注:无需再次复制整个代码,只需声明硬编码值有效即可。简单地说就足够了。它不起作用
不仅是非特定的,它也不像程序员。你必须非常具体地说明什么是不起作用的@Matthew