visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Knowledge Base : an "up-down directories" directory browsing script in PHP


By: VGR France  Date: 31/03/2005 15:59:02  English  Points: 0 Status: Answered
Quality : Good
Dated 14/04/2003

<?php $DEBUG=FALSE; if (isset($_GET['nextdir'])) $nextdir = $_GET['nextdir']; if (isset($_GET['userdir'])) $userdir = $_GET['userdir']; // for the first time only (session var afterwards) // delete the above line if the $userdir may be known as a session var since the beginning of the script (better security) if (!isset($nextdir)) $nextdir='/'; else $nextdir = urldecode($nextdir); // GIVEN : $userdir (=user name) is somewhat transmitted, via a session variable usually. session_start(); if (!isset($_SESSION['userdir'])) $_SESSION['userdir']=$_GET['userdir']; // first time it's in the URL... function nextdiris($path,$file) { GLOBAL $DEBUG; if ($file=='..') { // let's work if (($path=="")or($path=='/')) $poubStr='/'; else { if ($DEBUG) echo ".. for path='$path' "; //let's get over up level $zar=explode('/',$path); $j=count($zar)-1; while ($zar[$j]=="") $j--; $zar[$j]=""; // empty last component // no because array count stays unchanged... $poubStr=implode('/',$zar); $poubStr=$zar[0]; // can not be empty unless path was empty itself for ($j=1;$j<count($zar);$j++) if ($zar[$j]<>"") $poubStr.='/'.$zar[$j]; if ($DEBUG) echo ".. for path : result '$poubStr' "; } // if already at the top } else $poubStr=(($path=='/')?$file:$path.'/'.$file); return urlencode($poubStr); } // nextdiris String Function // dir to open ############################################################################################ $rootdir ='i:/www'; // no trailing slash // "d:/website_development/root"; $userrootdir = $rootdir.'/'.$userdir; if ($DEBUG) echo "userrootdir='$userrootdir' "; if (($nextdir=='/')or($nextdir=="")) { $diropen=$userrootdir; $curdir=$userdir; } else { $diropen=$userrootdir.'/'.$nextdir; $curdir=$userdir.$nextdir; } // selon le cas if ($DEBUG) echo "diropen= '$diropen' "; if ($DEBUG) echo "curdir= '$curdir' "; // end of dir to open ###################################################################################### // set strings ################ $total=0; $nbdir=0; $nbfile=0; $fdtotal=0; // Open and read directory ################################################################### //////////////////////////// html stuff normally //VGR12042003 ADDed this for test display echo "<TABLE>"; if ($handle = opendir($diropen)) { //VGR12042003 FIXed unnecessary quotes while ($file = readdir($handle)) { //VGR12042003 FIXed (removed) silly "false !== " if ($file != ".") { //VGR12042003 FIX parenthesing and chose clearer Boolean operator, FIXed indenting, ADDed .. case if (!( ($file=='..') AND ($nextdir=='/') )) { $checkdir = $diropen.'/'.$file; $isdir = is_dir($checkdir); // File or directory listing column ################################################################ //////////////////////////// html stuff normally //VGR12042003 ADDed this for test display echo "<TR><td>"; if ($isdir){ $a=nextdiris($nextdir,$file); if ($a<>'%2F') echo "&nbsp;&nbsp;<a href='$SCRIPT_NAME?nextdir=$a'><font color=red>".(($file != "..")?$file:'UP')."</font></a></b>"; //VGR12042003 ADDed colour to better test directories found echo "</font></font></td>"; } else { //if ($DEBUG) echo "file '$file' : curdir= '$curdir' "; echo "&nbsp;&nbsp;<a href='/$curdir/$file'>$file</a></b>"; echo "</font></font></td>"; } // File or directory size ###################################################################### echo "<td height='20' width='200' align='right'>"; echo "<font face='Arial, Helvetica, sans-serif'><font size='1'>"; $openfilesize = $diropen.'/'.$file; $fsize = filesize($openfilesize); if (($fsize =='0') AND (! $isdir)) { //ditto on test echo "0 bytes"; } else if($isdir) { // ditto on ==1 if ($a<>'%2F') echo "<b><u>Directory</u></b>"; } else { echo "".round($fsize/1.024)." bytes"; } // if isdir if ($isdir) $nbdir++; // ditto on ==1 $total += $fsize; if (! $isdir) $nbfile++; // ditto on ==0 $fdtotal++; } // if fsize and isdir echo "</font></font></td>"; } // if not current root and .. } // if not . //VGR12042003 ADDed this for test display echo "</tr>"; } // while //VGR12042003 ADDed this for test display echo "</TABLE>"; ?>


Do register to be able to answer

EContact
browser fav
page generated in 335.284950 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page