0

Is it possible to list down all files from a folder then have an option to delete them one by one?if so, can you give me the right way to do it?not the code but the step by step tutorial or explanation..well if its a code I just hope I understand it.LOL..i have read about unlink function, readdir but i cant seem figure it out how im gonna start with it...(currently experimenting on it)

4
Contributors
7
Replies
41
Views
2 Years
Discussion Span
Last Post by diafol
0

I used scandir but I dont want the directory to show...heres the working code so far:

    <ul>
    <?php echo "List of files"?>
    <?php
    $dir = 'kcfinder/upload/files';
    $files = scandir($dir);

    foreach($files as $file){
    ?>

    <li><?php echo $file;?> </a> | <a href="includes/delete.php?file=<?=$file?>">Delete</a></li>
    <?php
    }
    ?>
    </ul>
0

This assumes that the delete.php file knows the location of the file ('kcfinder/upload/files'). Will this always be the case?

0

What about something linke this:

$dir = scandir(dirname(realpath(__FILE__)));
$pattern = "^\/(.+\/)*(.+)\/^";

foreach($dir as $file){
    $file = preg_replace($pattern, '', $file);

    if( $file != '.' && $file != '..' ){
        echo $file . '<br />';
    }
}

This grabs all the files and folders in the current directory scripts out the file path.

Side Note: if you want to display files only you will need to adjust the regex pattern.

0

assuming you already have the list of file like this

 foreach($files as $file){
  echo $file ." | <a href='delete.php?file=$file'>Delete</a><br/>";
 }

delete.php
also check dir/file permission to other allow read/access/delete

 <?php
 $file = "path/to/file/$_GET[file]";
 if (!unlink($file)) {
      echo ("Error deleting $file");
 } else {
  echo ("Deleted $file");
 } 
 ?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.