php - 传递连接字符串时,我无法在 PHP 中删除目录?
问题描述
我在我的 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);
// 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);
}
我尝试使用 === 运算符比较 2 个字符串,它返回 1。不知道为什么会这样。
解决方案
推荐阅读
- google-cloud-platform - 在 Google Compute Engine 上打开端口 8080
- javascript - 单击插槽时在反应大日历中显示预订模式并将预订按钮保留在空插槽中
- lisp - 是否存在插槽不会被初始化为其 initform 的情况?
- ruby-on-rails - 无法运行资产:在heroku上预编译
- c++ - 将输入值存储到数组中,同时读取它们,c++
- swift - 如何在 Swift 4.2 中捕获字符串中的单个字符并将它们放入数组中?
- python - 使用过滤和递归的随机顺序生成器
- spring - 如何在不使用 Spring Security 中的 xml 的情况下将拦截器与 URL 匹配?
- swagger - 在 JHipster 服务器和微服务中提供 Swagger-UI
- javascript - 带逗号和两位小数的数值格式