note 61012 added to function.opendir

From: Date: Mon, 23 Jan 2006 15:04:26 +0000
Subject: note 61012 added to function.opendir
Groups: php.notes 
Request: Send a blank email to php-notes+get-102299@lists.php.net to get a copy of this message
I Just wanted a directory list and a clickable link to download the files because my plesk server does not give me this function. I edited the script a little bit. Many thanks from a script-noob ------ <? echo ("<h1>Directory Overzicht:</h1>"); function getFiles($path) { //Function takes a path, and returns a numerically indexed array of associative arrays containing file information, //sorted by the file name (case insensitive). If two files are identical when compared without case, they will sort //relative to each other in the order presented by readdir() $files = array(); $fileNames = array(); $i = 0; if (is_dir($path)) { if ($dh = opendir($path)) { while (($file = readdir($dh)) !== false) { if ($file == "." || $file == "..") continue; $fullpath = $path . "/" . $file; $fkey = strtolower($file); while (array_key_exists($fkey,$fileNames)) $fkey .= " "; $a = stat($fullpath); $files[$fkey]['size'] = $a['size']; if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-"; else if ($a['size'] > 1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K"; else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb"; else $files[$fkey]['sizetext'] = $a['size'] . " bytes"; $files[$fkey]['name'] = $file; $files[$fkey]['type'] = filetype($fullpath); $fileNames[$i++] = $fkey; } closedir($dh); } else die ("Cannot open directory: $path"); } else die ("Path is not a directory: $path"); sort($fileNames,SORT_STRING); $sortedFiles = array(); $i = 0; foreach($fileNames as $f) $sortedFiles[$i++] = $files[$f]; return $sortedFiles; } $files = getFiles("./"); foreach ($files as $file) print "&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"$file[name]\">$file[name]</a></b><br>\n"; ?> ---- Server IP: 194.109.193.119 Probable Submitter: 81.69.16.146 ---- Manual Page -- http://www.php.net/manual/en/function.opendir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61012 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61012&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61012&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61012&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61012&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61012&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61012&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61012&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61012&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102299) next »