Reputation: 571
I am listing the text files that I have on my server, and I want to add a link to download that file and/or delete it.
It works perfectly, but the problem is that when I press the delete button it deletes the file and opens a new page, and what I want is that when I press the delete button, deletes the file and refresh the page.
This is my code:
$user = $_SESSION['email'];
$direc = "./WSN/" . $user;
$directorio = opendir($direc); //current route
$vacio = true;
while ($archivo = readdir($directorio)) //get a file and then another on
{
if (($archivo!=".")&&($archivo!=".."))//check whether or not a directory
{
echo "
<b>
<a href=\"".$direc."/".$archivo."\" target=\"_blank\">".$archivo."</a>
</b>
-
<b>
<a href=\"include/download.php?file=.".$direc."/".$archivo."\" title='download'>Download</a>
</b>
-
<b>
<a href=\"include/delete.php?file=.".$direc."/".$archivo."\" title='delete'>Delete</a>
</b>
<br />
";
$vacio = false;
}
}
And this is the code of delete.php:
$file = $_GET['file'];
unlink($file);
Someone can help me?
[Edit]: "abreuna" means "opens one".
Upvotes: 0
Views: 490
Reputation: 306
Why don't you simply add to your delete.php, after the unlink($file):
header('Location: list.php');
Assuming that your first code snippet is on a file named list.php
BTW, be aware that you delete.php opens a big security hole. Anyone can ask your script to delete any file that your php process can access.
The same goes to download, anyone can download files you don't want to be downloaded.
Upvotes: 2