首页 > 解决方案 > 传递连接字符串时,我无法在 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。不知道为什么会这样。

标签: phpwindowswebdirectory

解决方案


推荐阅读