0

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>';    
        }
    }
}
George Brighton
  • 5,131
  • 9
  • 27
  • 36

2 Answers2

1

You can use unlink() php function

Ref: http://www.php.net/unlink

<?php
   $mask = "*.jpg"
  array_map( "unlink", glob( $mask ) );
?>
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • I tried that but it does not link to any file, as I am listing the files with the `while` loop –  Dec 19 '13 at 04:36
  • Make sure you have neccessary permissions to delete the file – Krish R Dec 19 '13 at 04:37
  • I have all the permissions, It is that it does not know the file type or any informaion about the file in general. Remember the user is able to upload any file they want. –  Dec 19 '13 at 04:38
  • It doesn't need to know anything about the file type other than it's exact path and it's name. – James Binford Dec 19 '13 at 04:47
  • if you have the permissions then `unlink` will work. if it doesn't then something else is is awry – gwillie Dec 19 '13 at 04:53
0
$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";
}
  • replace SamePageURL.php with your PHP page name. – Vijayakumar Selvaraj Dec 19 '13 at 04:57
  • Now I get cannot modify header –  Dec 19 '13 at 04:59
  • Nevermind, I can just turn the error off for that part. Thanks –  Dec 19 '13 at 04:59
  • OK... refer this http://stackoverflow.com/questions/1793482/php-error-cannot-modify-header-information-headers-already-sent?rq=1. use ob_start() at top of your page. – Vijayakumar Selvaraj Dec 19 '13 at 05:01
  • You were fast at answering this, Do you think you could take a look at another question I had. I want to download the file on click: http://stackoverflow.com/questions/20605867/how-can-i-download-a-file-with-php-or-html –  Dec 19 '13 at 05:06
  • Will not download upon clicking the file name –  Dec 19 '13 at 05:22