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:
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.
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.