<?php
// recup nom dossier parent
function my_dir(){
$arfdn = explode('/', dirname($_SERVER['PHP_SELF']));
return end($arfdn);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FTP Index de <?php echo my_dir(); ?></title>
<link rel="stylesheet" type="text/css" media="all" href="XXX" />
</head>
<body>
<div id="ftp-wrapper">
<h1>Contenu du répertoire :
<?php
echo my_dir(); // nom dossier parent
?>
</h1>
<div class="notes">
<p>N-B - Ce contenu a un caractère privé et provisoire.</p>
<p>Me confirmer par courriel que vous avez bien récupéré les fichiers.</p>
</div>
<table>
<?php
// returns the extension of a file
function strip_ext($name)
{
$ext = substr($name, strlen($ext)-4, 4);
if(strpos($ext,'.') === false) // if we have a folder element
{
return " "; // we return a string of space characters for later sort,
// so that the folder items remain on the first positions
}
return $ext; // if we have a file we return the extension - .gif, .jpg, etc.
}
// finds extention of file
function findexts($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
// returns the files from the $path and returns them in an array
function getFiles($path) {
$files = array();
$fileNames = array();
$i = 0;
// build
if (is_dir($path)) {
if ($dh = opendir($path)) {
while (($file = readdir($dh)) !== false) {
if (($file == ".") || ($file == "..")) continue;
$fullpath = $path . "/" . $file;
//$fkey = strtolower($file);
$fkey = $file;
while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
$a = stat($fullpath);
$files[$fkey]['size'] = $a['size'];
if ($a['size'] == 0) $files[$fkey]['sizetext'] = "vide";
else if ($a['size'] > 1024 && $a['size'] <= 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " ko";
else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mo";
// else $files[$fkey]['sizetext'] = $a['size'] . " octets";
else $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " ko";
$files[$fkey]['name'] = $file;
$e = strip_ext($file); // $e is the extension - for example, .gif
$files[$fkey]['type'] = filetype($fullpath); // file, dir, etc
$k=$e.$file; // we use this string for sorting the array elements by extension and filename;
$fileNames[$i++] = $k;
}
closedir($dh);
} else die ("Cannot open directory: $path");
} else die ("Path is not a directory: $path");
sort($fileNames,SORT_STRING); // sorting
$sortedFiles = array();
$i = 0;
foreach($fileNames as $f) {
$f = substr($f, 4, strlen($f)-4); // we remove the extension we added in front of the filename for sorting
if($files[$f]['name'] !='') $sortedFiles[$i++] = $files[$f];
}// ends the foreach where we build the final sorted array
return $sortedFiles;
}
// folder navigation code
$startdir = "./";
if(isset($_GET['dir'])) {
$prev = $_GET['dir'];
$folder = $_GET['dir'];
echo "<a href=\"javascript:history.go(-1)\">Retour</a>";
} else { $folder = $startdir; $prev='';}
// end folder navigation code
$files = getFiles($folder);
foreach ($files as $file) {
if(strip_ext($file[name])!='.php'){
if($file[type]=='dir') {
$cmd='?dir='.$prev.$file[name].'/';
}// if the element is a directory
else $cmd=$prev.$file[name];
echo "<tr class=\"ftpico-";
if($file[type]=='dir') {
echo "dir";
}
else echo findexts($file[name]);
echo "\"><td><a href=\"$cmd\" > $file[name]</a></td><td class=\"notes\" style=\"text-transform:uppercase\">";
if($file[type]=='dir') {
echo "—";
}
else echo findexts($file[name]);
echo "</td><td class=\"notes\">$file[sizetext]</td></tr>";
}//if strip_ext
}//foreach
?>
</table>
<div class="notes">
<p><a href="/">Page d'accueil</a></p>
</div>
</div>
</body>
</html>