I am using the deleteFiles() function in my PHP program to delete directories. It only works when I pass in explicitly defined strings but not when I pass in a string concatenated with variables.
This doesn't work:
// 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);
}
This does work:
// 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);
}
I tried comparing the 2 strings using the === operator and it returns 1. No idea why this is happening.
