Unlink selected files php

This tutorial explains how to unlink selected files php.On the button submit it retrieve all the files from the checkbox array.The foreach loop it iterate through the array and delete the selected file if that exists.

Refer the tutorial part one which explains how to read all files in a folder. It list all the files in folder as table using php.

  public function selectedFiles() {
        
         if(isset($_REQUEST['delete'])) {
            if( sizeof( $_POST['selectedFiles'] ) > 0) {              
                $this->deleteFiles( $_POST['selectedFiles'] );
            }
        }
        
    }
    
    public function deleteFiles($selectedFiles) {
        
        foreach( $selectedFiles  as $selectedFile ) {
            $path= "upload/".$selectedFile;
            if (file_exists($path)) {
                if( unlink( $path ) ) 
                    echo "Deleted file " .$selectedFile."\n";
            } 
            else {
                echo "The file $path does not exist";
            }            
        }
        
    }

Share this Tutorial