Re: [PHP] Menu from Directory
Ben Whitehead wrote: Having looked at it, it seems to be along the lines of what I was thinking of... I would be very grateful if you could post the code, since I do think it will be very useful. The main difference I was thinking of was to have a menu in the form of a page of links, rather than a drop-down menu, but having seen that, I would much prefer the drop-down menu approach!!! Thanks very much, Ben. Sorry I didn't get to this in a while -- had to fix my filters :) Anyhow, the functions that I used to create my drop down box : function getSubDir($rootdirpath) { if ($dir = @opendir($rootdirpath)) { $array[] = $rootdirpath; while (($file = readdir($dir)) !== false) { if (is_dir($rootdirpath."/".$file) && $file != "." && $file != "..") { $array = array_merge($array,getSubDir($rootdirpath."/".$file)); } } closedir($dir); } return $array; } function getFiles($dirname) { $handle=opendir($dirname); while ($file = readdir($handle)) { if($file=='.'||$file=='..') continue; else $result_array[]=$file; } closedir($handle); return $result_array; } The first function returns all the subdirs in a directory (given as $rootdirpath). The second functions returns an array of all files in a directory. Simply combine these two functions to get a listing of all files in subdirectories : $subdirs = getSubDir("/path/to/main"); //no trailing / array_shift($subdirs); //gets rid of the parent dir $count = 0; while (list(,$val) = each($subdirs)) { $files[substr(strrchr($val,"/"),1,strlen(strrchr($val,"/")))] = getFiles($val); $count += count(getFiles($val)); } This while loop gets a list of all the files and counts the total number of files in the subdirs. The list of files is in an array called $files. Now, to build the drop down : while(list($key,$val) = each($files)) { echo "Section $key :\n"; while(list(,$name) = each ($val)) { echo " -=> "; echo substr($name,0,strlen($name)-4); echo "\n"; } echo "\n"; // space } I hope this helps you out. Let me know if you can't understand anything :) -- Burhan Khalid phplist[at]meidomus[dot]com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Menu from Directory
Ben Whitehead wrote: I am trying to sort an image menu system which is passed a directory name, and can count and name the files in a menu from simply seeing which files are in this directory. It would be ideal if it can just do this just by looking at the files in the directory, and their file names, but if that is not possible, could it us the information in a text file I have created and placed in this directory? Anyway help would be greatly received! (I'm new to PHP, so simple help would be even more appreciated!) :S I wrote some code that might be what you want. It was for a reference download system. Here is what it does : 1. Scans a directory for all sub-directories 2. Scans those subdirectoties for files 3. Creates a drop down box with the appropriate categories (sub dir name ), and file listing. You can see it in action at http://www.meidomus.com (top left, marked REFERENCE DOWNLOAD) Let me know if this is what you were after and I will post the code. Its just two functions. -- Burhan Khalid phplist[at]meidomus[dot]com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Menu from Directory
You might want to start with searching the manual for opendir() function. There is a sample code listing all files from a directory. :) Niklas -Original Message- From: Ben Whitehead [mailto:[EMAIL PROTECTED] Sent: 2. huhtikuuta 2003 1:05 To: [EMAIL PROTECTED] Subject: [PHP] Menu from Directory I am trying to sort an image menu system which is passed a directory name, and can count and name the files in a menu from simply seeing which files are in this directory. It would be ideal if it can just do this just by looking at the files in the directory, and their file names, but if that is not possible, could it us the information in a text file I have created and placed in this directory? Anyway help would be greatly received! (I'm new to PHP, so simple help would be even more appreciated!) :S Ben -- Ben Whitehead [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ### This message has been scanned by F-Secure Anti-Virus for Internet Mail. For more information, connect to http://www.F-Secure.com/ ### This message has been scanned by F-Secure Anti-Virus for Internet Mail. For more information, connect to http://www.F-Secure.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php