简体   繁体   中英

php file not unlinking

I am calling a .php file that lies within my 'uploads' directory to delete from the filesystem. The directories that need to be searched will always be inside "uploads/$_SESSION['email']". This is what I have currently:

<?php

session_start();

require('../mysqli_connect.php'); 


    $old = getcwd(); // Save the current directory
    $new = './'.$_SESSION['email'];
    chdir($new);

    $delpaths = "SELECT `title` FROM `upload` WHERE `upload_id` IN ({$id_array}) AND `owner_id` =". $_SESSION['user_id'];
    $getpaths = mysqli_query($dbc, $delpaths);
    while ($row = mysqli_fetch_array($getpaths, MYSQLI_ASSOC))
    {
        chown($row, 666);

function removeupload() {
$todelete = $row;
unlink($todelete);
chdir($old); 

}

    }


?>

I see a of couple issues here... The first being that $row would be passed back as an array, so simply calling $row won't give you the desired result (which I'm assuming is the name of the file you're trying to delete that is stored in your database). $row should be changed to something like:

chown($row['column_name'], 666); // Where column_name is the name of the file you're trying to delete

The second is that there is nothing being passed to your function . You should rewrite it so that you can pass a couple variables to it so that it executes properly, we'll add $filename and $old.

function removeupload($filename, $old) {
    unlink($filename);
    chdir($old); 
}    

Finally, you should define this function outside of your while loop , usually at the beginning of the page, and call it when you'd want the desired effect to happen.

Your final code should should look something like this.

<?php
    //Start the session
    session_start();

    //Include the connection script
    require('../mysqli_connect.php'); 

    //Build the file deletion function
    function removeupload($filename, $old) {
        unlink($filename);
        chdir($old); 
    }  

    $old = getcwd(); // Save the current directory
    $new = './'.$_SESSION['email'];
    chdir($new);

    $delpaths = "SELECT `title` FROM `upload` WHERE `upload_id` IN ({$id_array}) AND `owner_id` =". $_SESSION['user_id'];
    $getpaths = mysqli_query($dbc, $delpaths);

    while ($row = mysqli_fetch_array($getpaths, MYSQLI_ASSOC)) {
        chown($row['column_name'], 666); //This $row column holds the file name, whatever you've called it in your database.

        //Now call the function and pass the variables to it
        removeupload($row['column_name'], $old);

    }

?>
<?php

session_start();

require('../mysqli_connect.php'); 

function removeupload($row , $old){
    $todelete = $row;
    unlink($todelete);
    chdir($old);
}

    $old = getcwd(); // Save the current directory
    $new = './'.$_SESSION['email'];
    chdir($new);

    $delpaths = "SELECT `title` FROM `upload` WHERE `upload_id` IN ({$id_array}) AND `owner_id` =". $_SESSION['user_id'];
    $getpaths = mysqli_query($dbc, $delpaths);
    while ($row = mysqli_fetch_array($getpaths, MYSQLI_ASSOC))
    {
        chown($row, 666);
        removeupload($row['title'] , $old);
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM