VIVI WILL
VIVI WILL

Reputation: 41

How to write a PHP coding to list out all files and directories as links to them?

This is somewhat similar to some index pages. When new file or folder is added to the directory, HTML page should display the newly created file/folder together with previous ones after it is being refreshed. (prefer in alphabatical order)

How to achieve this sort of functionality in PHP? Please provide sample coding as well. (and any references)

Upvotes: 4

Views: 22337

Answers (2)

Mark Elliot
Mark Elliot

Reputation: 77064

Pretty much a direct rip from the manual:

<?php
$d = dir(".");
echo "Path: " . $d->path . "\n";
echo "<ul>";
while (false !== ($entry = $d->read())) {
   echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();
?>

Upvotes: 4

Frantisek
Frantisek

Reputation: 7693

this is rather easy, here you go:

$files = scandir('./directory/to/list');
sort($files); // this does the sorting
foreach($files as $file){
   echo'<a href="/directory/to/list/'.$file.'">'.$file.'</a>';
}

this should give you a basic idea what to do.

Upvotes: 17

Related Questions