1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| <?php
// Renseigner la variable $dossier avec la bonne valeur
$dossier = 'DOSSIER_TEST';
// Filtre les entrées qui ne sont pas des fichiers ou les fichiers nommés .htaccess
class FiltreFichiers extends FilterIterator {
public function accept() {
$it = parent::current();
return $it->isFile() && $it->getFilename() != '.htaccess';
}
}
class FichiersListeSpl
{
// nom de la session si non définie en deuxième argument du constructeur
private $nom_session = 'tab_fichiers_upload';
private $erreur;
public $dossier;
public $tab_fichier = array();
public function __construct($dossier,$nom_session=null)
{
try
{
if(is_dir($dossier)) $this->dossier = $dossier;
else throw new Exception ("dossier '$dossier' non valide");
$ressource = new FiltreFichiers(new FilesystemIterator($this->dossier));
$this->tab_fichier = $this->createTabSession($ressource,$nom_session);
}
catch (Exception $e)
{
$this->erreur = 'Erreur : '.($e->getMessage());
}
$this->dossier = isset($this->erreur) ? $this->erreur : basename($this->dossier);
}
// Met en session les attributs nécessaires pour le téléchargement (key->path, value->size)
private function createTabSession($ressource_spl, $nom_session)
{
if (!empty($nom_session)) $this->nom_session = $nom_session;
if (!session_id()) session_start();
$_SESSION[$this->nom_session] = array();
foreach ($ressource_spl as $fileinfo)
{
$_SESSION[$this->nom_session][$fileinfo->getPathname()] = $fileinfo->getSize();
}
return $_SESSION[$this->nom_session];
}
}
$list = new FichiersListeSpl($dossier);
$titre_dossier = $list->dossier;
$tab_result = $list->tab_fichier;
ksort($tab_result, SORT_NATURAL | SORT_FLAG_CASE);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Document sans titre</title>
<style type="text/css">
body {
font-family:Arial, Helvetica, sans-serif;
}
.liens li a {
font-style:normal;
text-decoration:none;
color: #666;
}
</style>
</head>
<body>
<h3><?= $titre_dossier?></h3>
<div>
<ul class="liens">
<?php
foreach ($tab_result as $path => $value)
{
echo '<li><a href="telechargement.php?fich='.$path.'">'.basename($path).'</a></li>';
}
?>
</ul>
</div>
</body>
</html> |
Partager