Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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)

like image 645
VIVI WILL Avatar asked Dec 03 '22 02:12

VIVI WILL


2 Answers

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.

like image 58
Frantisek Avatar answered Apr 29 '23 03:04

Frantisek


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();
?>
like image 38
Mark Elliot Avatar answered Apr 29 '23 04:04

Mark Elliot