List all files in one directory PHP

ID : 10252

viewed : 20

Tags : phplistdirectoryphp

Top 5 Answer for List all files in one directory PHP

vote vote


You are looking for the command scandir.

$path    = '/tmp'; $files = scandir($path); 

Following code will remove . and .. from the returned array from scandir:

$files = array_diff(scandir($path), array('.', '..')); 
vote vote


Check this out : readdir()

This bit of code should list all entries in a certain directory:

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

Edit: miah's solution is much more elegant than mine, you should use his solution instead.

vote vote


A clean way to delete a cookie is to clear both of $_COOKIE value and browser cookie file :

if (isset($_COOKIE['key'])) {     unset($_COOKIE['key']);     setcookie('key', '', time() - 3600, '/'); // empty value and old timestamp } 
vote vote


To reliably delete a cookie it's not enough to set it to expire anytime in the past, as computed by your PHP server. This is because client computers can and often do have times which differ from that of your server.

The best practice is to overwrite the current cookie with a blank cookie which expires one second in the future after the epoch (1 January 1970 00:00:00 UTC), as so:

setcookie("hello", "", 1); 
vote vote


That will unset the cookie in your code, but since the $_COOKIE variable is refreshed on each request, it'll just come back on the next page request.

To actually get rid of the cookie, set the expiration date in the past:

// set the expiration date to one hour ago setcookie("hello", "", time()-3600); 

Top 3 video Explaining List all files in one directory PHP