I’m a beginner with php and have created a code displaying a table with the data concerning my users, that is in my database.
This is the code I used.
Name
Surname
Date of Birth
Town of Birth
Code
Address
E-mail
“;
while($row=mysql_fetch_array($result))
{echo"<tr>";
echo"<td>" . $row['Name'] . "</td>";
echo"<td>" . $row['Surname'] . "</td>";
echo"<td>" . $row['DateofBirth'] . "</td>";
echo"<td>" . $row['TownofBirth'] . "</td>";
echo"<td>" . $row['Code'] . "</td>";
echo"<td>" . $row['Address'] . "</td>";
echo"<td>" . $row['Email'] . "</td>";
echo'<td><a href="delete.php?id='.$row['Code'].'">Delete</a></td>';
echo"</tr>";
}
echo"</table>";
?>
I also have created a delete.php file which allows to delete users from the db by clicking on the “delete” link, but I also wanted this code to remove a folder named with the username of the user, that is situated in the current work directory. This is the code I wrote:
<?php
$con=mysql_connect("localhost","root","mypassword");
mysql_select_db("mydb",$con);
function deletedir($dirname){
if(file_exists($dirname) && is_file($dirname)) {
unlink($dirname);
} elseif(is_dir($dirname)) {
$handle=opendir($dirname);
while(false!==($file=readdir($handle))){
if(is_file($dirname.$file)){
unlink($dirname.$file);
}
}
$handle=closedir($handle);
rmdir($dirname);
}
}
if (isset($_GET['id'])) {
$id=$_GET['id'];
$sql=mysql_query("SELECT Username FROM Users WHERE Code='$id'");
while($row=mysql_fetch_array($sql)) {
$username=$row['Username'];
deletedir($username);
}
$result=mysql_query("DELETE FROM Users WHERE Code='$id'") or die(mysql_error());
header("location: index.php");
} else {
header("location: index.php");
}
?>
This code deletes the user from the db but it doesn’t remove the folder called $username. Please help me to understand where the error is. Thank you in advance.
I think this is your problem:
unlinkandrmdirneed a full path to the files/directories to remove if the deleting script isn’t in the same directory as what you are trying to delete.Try adding something like this to the top of your deletedir function:
Change “/opt/www/userdirs” to the directory where your directories to delete reside.
And I think that you also need to change all of these:
To: