Reputation:
I am trying to make a simple file hosting site for my family, a couple friends and I to use. I have the entire system setup except one thing... I cannot find a way to let the user delete a file without having to access the FTP. I will post the code in which I use to list the files to the user below. I want to have a delete button automatically generated for each new file the user uploads.
Code to list files:
$directory = 'uploads/' . $_SESSION['user'] . '/';
if ($handle = opendir($directory)) {
echo '<h3>Your files are listed below</h3>';
while ($file = readdir($handle)) {
if ($file != '.' && $file != '..') {
echo '<a target="_blank" href="' . $directory . '/' . $file . '">' . $file . '<br>';
}
}
}
Upvotes: 0
Views: 206
Reputation: 523
$directory = 'uploads/' . $_SESSION['user'] . '/';
if(isset($_REQUEST['DelFile'])) {
$DeleteFile = $_REQUEST['DelFile'];
if(file_exists($directory.$DeleteFile)) {
@unlink($directory.$DeleteFile);
header("location:SamePageURL.php?msg=1");
} else header("location:SamePageURL.php?msg=2");
}
if ($handle = opendir($directory)) {
echo '<h3>Your files are listed below</h3>';
while ($file = readdir($handle)) {
if ($file != '.' && $file != '..') {
echo '<a target="_blank" href="'.$directory.'/'.$file.'">' . $file.' <a href="SamePageURL.php?DelFile='.$file.'">Delete</a> <br>';
}
}
}
if(isset($_REQUEST['msg'])) {
$Message = $_REQUEST['msg'];
if($Message == 1) echo "File deleted sucessfully";
else if($Message == 1) echo "File not found";
}
Upvotes: 0
Reputation: 22721
You can use unlink()
php function
Ref: http://www.php.net/unlink
<?php
$mask = "*.jpg"
array_map( "unlink", glob( $mask ) );
?>
Upvotes: 1