par
Nagol » 26 mars 2009, 06:00
Bah j'ai relu, et ensuite je me suis dit que j'avais pas compris la question ceci dit tu pourrais utiliser mon premier script et faire un truc en javascript tout mignon, dans le cas contraire tu pourrais utiliser ça:
<?php
function listcurdir($root='root', $dir='root', $debug=false) {
$result = array();
if($dir == 'root' || is_null($dir)) {
if($root != 'root') {
$dir = $root;
} else {
$dir = dirname(__FILE__);
}
} else {
$dir = base64_decode(urldecode($dir));
}
if($root == 'root' || is_null($root)) {
$root = dirname(__FILE__);
}
if(!preg_match('`^'.$root.'`', $dir)) {
if($debug) {
echo 'Error: ' . $dir . ' isnt in '. $root . '!' . "\n";
}
return $result;
}
$dir = trim($dir);
$root = trim($root);
if(DIRECTORY_SEPARATOR == '/') {
$dir = str_replace('\\', DIRECTORY_SEPARATOR, $dir);
$root = str_replace('\\', DIRECTORY_SEPARATOR, $root);
} else {
$dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
$root = str_replace('/', DIRECTORY_SEPARATOR, $root);
}
$dir = str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $dir);
$root = str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $root);
if($debug) {
echo 'Requested: "' . $dir . '"'."\n";
}
$dir = realpath($dir);
$root = realpath($root);
$tmp = explode(DIRECTORY_SEPARATOR, $dir);
array_pop($tmp);
$prevdir = implode(DIRECTORY_SEPARATOR, $tmp);
if($dir[strlen($dir - 1)] != DIRECTORY_SEPARATOR) {
$dir .= DIRECTORY_SEPARATOR;
}
if($root[strlen($root - 1)] != DIRECTORY_SEPARATOR) {
$root .= DIRECTORY_SEPARATOR;
}
if($prevdir[strlen($prevdir - 1)] != DIRECTORY_SEPARATOR) {
$prevdir .= DIRECTORY_SEPARATOR;
}
if($debug) {
echo 'Scanning: "' . $dir . '"' . "\n";
}
if($root == $prevdir || preg_match('`^'.$root.'`', $prevdir)) {
array_push($result, array('link'=>urlencode(base64_encode($prevdir)), 'type'=>'D', 'name'=>'..'));
}
$curlist = glob($dir . '*');
for($i=0; $i<count($curlist); $i++) {
$type = 'U';
if(is_dir($curlist[$i])) {$type = 'D';}
if(is_file($curlist[$i])) {$type = 'F';}
if(is_link($curlist[$i])) {$type = 'L';}
array_push($result, array('link'=>urlencode(base64_encode($curlist[$i])), 'type'=>$type, 'name'=>str_replace($dir, '', $curlist[$i])));
}
return $result;
}
if(isset($_GET['dir'])) {
$dir = $_GET['dir'];
} else {
$dir = null;
}
$list = listcurdir('/home/logan', $dir);
function cmp($a, $b){
if($b['name'] == '..') return +1;
if ($a['type'] == $b['type']) {
if(strtolower($a['name']) == strtolower($b['name'])) {
return 0;
} else {
return (strtolower($a['name']) > strtolower($b['name'])) ? +1 : -1;
}
}
return ($a['type']=='D' && ($b['type'] == 'F' || $b['type'] == 'L')) ? -1 : +1;
}
usort($list, "cmp");
echo '<ul>' . "\n";
foreach($list as $file) {
if($file['type'] == 'D') {
$text = '<a href="'.$_SERVER['PHP_SELF'].'?dir=' . $file['link'] . '">'.$file['name'].'</a>';
} else {
$text = $file['name'];
}
echo '<li>('.$file['type'].') ' . $text . '</li>' . "\n";
}
echo '</ul>' . "\n";
Bah j'ai relu, et ensuite je me suis dit que j'avais pas compris la question ceci dit tu pourrais utiliser mon premier script et faire un truc en javascript tout mignon, dans le cas contraire tu pourrais utiliser ça:
[php]
<?php
function listcurdir($root='root', $dir='root', $debug=false) {
$result = array();
if($dir == 'root' || is_null($dir)) {
if($root != 'root') {
$dir = $root;
} else {
$dir = dirname(__FILE__);
}
} else {
$dir = base64_decode(urldecode($dir));
}
if($root == 'root' || is_null($root)) {
$root = dirname(__FILE__);
}
if(!preg_match('`^'.$root.'`', $dir)) {
if($debug) {
echo 'Error: ' . $dir . ' isnt in '. $root . '!' . "\n";
}
return $result;
}
$dir = trim($dir);
$root = trim($root);
if(DIRECTORY_SEPARATOR == '/') {
$dir = str_replace('\\', DIRECTORY_SEPARATOR, $dir);
$root = str_replace('\\', DIRECTORY_SEPARATOR, $root);
} else {
$dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
$root = str_replace('/', DIRECTORY_SEPARATOR, $root);
}
$dir = str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $dir);
$root = str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $root);
if($debug) {
echo 'Requested: "' . $dir . '"'."\n";
}
$dir = realpath($dir);
$root = realpath($root);
$tmp = explode(DIRECTORY_SEPARATOR, $dir);
array_pop($tmp);
$prevdir = implode(DIRECTORY_SEPARATOR, $tmp);
if($dir[strlen($dir - 1)] != DIRECTORY_SEPARATOR) {
$dir .= DIRECTORY_SEPARATOR;
}
if($root[strlen($root - 1)] != DIRECTORY_SEPARATOR) {
$root .= DIRECTORY_SEPARATOR;
}
if($prevdir[strlen($prevdir - 1)] != DIRECTORY_SEPARATOR) {
$prevdir .= DIRECTORY_SEPARATOR;
}
if($debug) {
echo 'Scanning: "' . $dir . '"' . "\n";
}
if($root == $prevdir || preg_match('`^'.$root.'`', $prevdir)) {
array_push($result, array('link'=>urlencode(base64_encode($prevdir)), 'type'=>'D', 'name'=>'..'));
}
$curlist = glob($dir . '*');
for($i=0; $i<count($curlist); $i++) {
$type = 'U';
if(is_dir($curlist[$i])) {$type = 'D';}
if(is_file($curlist[$i])) {$type = 'F';}
if(is_link($curlist[$i])) {$type = 'L';}
array_push($result, array('link'=>urlencode(base64_encode($curlist[$i])), 'type'=>$type, 'name'=>str_replace($dir, '', $curlist[$i])));
}
return $result;
}
if(isset($_GET['dir'])) {
$dir = $_GET['dir'];
} else {
$dir = null;
}
$list = listcurdir('/home/logan', $dir);
function cmp($a, $b){
if($b['name'] == '..') return +1;
if ($a['type'] == $b['type']) {
if(strtolower($a['name']) == strtolower($b['name'])) {
return 0;
} else {
return (strtolower($a['name']) > strtolower($b['name'])) ? +1 : -1;
}
}
return ($a['type']=='D' && ($b['type'] == 'F' || $b['type'] == 'L')) ? -1 : +1;
}
usort($list, "cmp");
echo '<ul>' . "\n";
foreach($list as $file) {
if($file['type'] == 'D') {
$text = '<a href="'.$_SERVER['PHP_SELF'].'?dir=' . $file['link'] . '">'.$file['name'].'</a>';
} else {
$text = $file['name'];
}
echo '<li>('.$file['type'].') ' . $text . '</li>' . "\n";
}
echo '</ul>' . "\n";
[/php]