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)

Recommended Answers

All 7 Replies

yes, thanks for the reply...much appreciated.:)

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>
Member Avatar for diafol

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

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.

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");
 } 
 ?>
Member Avatar for diafol

Perhaps we need to wait for RKTM to come back.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.