Site icon T4Tutorials.com

Source Code of View and Delete Files From a Directory using PHP

Source Code of View and Delete Files From a Directory using PHP

You can view files from a directory using the following two methods in PHP.

In the first method, we use opendir() and readdir() functions.

Example:

if ($handle = opendir('.')) 
	{
	    while (false !== ($entry = readdir($handle))) {
	        if ($entry != "." && $entry != "..") {
	            echo "$entry\n<br>";
	        }
	    }
	    closedir($handle);
	}

In the second method, we use scandir() function. This function returns the files from the directory in a form of an array.

Example:

<?php
$direc = "test/";
$asc = scandir($direc);
$des = scandir($direc,1);
print_r($asc);
print_r($des);
?>

Delete Files From The Directory – Source Code in PHP

You can delete files from the directory using unlink() function in PHP.

Example:

if(isset($_POST['del']))
{
 $FN = $_POST['FN'];
 unlink('test/'.$FN);
}

Output

View Method opendir() and readdir().

View Method scandir().

Delete Files.

Download – How to View and Delete Files From a Directory using PHP

 

Exit mobile version