phpretard Posted January 19, 2011 Share Posted January 19, 2011 I almost have it but can't figure why the code below only shows one file per folder. I think it is showing the last file. I need $ul somehow looping to show all the files in $file. <?php $year = date('Y'); if ($handle = opendir("results/$year/Boys")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if ($handle2 = opendir("results/$year/Boys/$file")) { while (false !== ($file2 = readdir($handle2))) { if ($file2 != "." && $file2 != "..") { $ul = "<li><a href=\"#\">$file2</a></li>"; $li = "<li><a target=\"_blank\" href=\"javascript:void(0)\">$file</a> <ul> $ul </ul> </li>"; } // close if file2 }echo $li; } } } }closedir($handle);closedir($handle2); ?> Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted January 19, 2011 Share Posted January 19, 2011 Out of curiosity why do you have 2 handles?? I am working on a file system class for a project here at work, this will get all the files from the directory passed: public function loadFiles($directory, $wantedExts) { /** * @var array $fileList a list of files in the directory */ $fileList = array(); $extension = explode(",", $wantedExts); echo count($extension); print_r ($extension); /* * Check to see if the current file in the directory is of the correct type. * * This method excludes . and .. listings to make sure they are not recursive and * files contained wihtin the directory. * * If it is what we want, push it on the $fileList array. * * @TODO: * * Accept multiple file names, like "csv,xls" */ // Check to see if the directory is valid and accessible if ($handle = opendir($directory)) { // Read the files in the directory while (false !== ($file = readdir($handle))) { /* * Not a directory or other level of the tree */ /* if ($file != "." && $file != ".." && $this->checkExt($file, $extention)) { array_push($fileList, $file); } */ if ($file != "." && $file != "..") { /* if (!empty($extention) && $this->checkExt($file, $extension)) { */echo $this->getExt($file); if (isset($extension) && (count($extension) > 0) && in_array($this->getExt($file), $extension, FALSE)) { array_push($fileList, $file); } else { /* * Get all files */ array_push($fileList, $file); } } } } else { die ("<div class='error'>Invalid directory!</div>"); } unset($extension); return $fileList; } Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted January 19, 2011 Share Posted January 19, 2011 It returns an array where you can just foreach through and print the li's in there Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted January 19, 2011 Share Posted January 19, 2011 You might need this /** * public function getExt($file) * * Returns a file's extention. * * @param string file to parse extention * @return string file's extention */ public function getExt($file) { return pathinfo($file, PATHINFO_EXTENSION); } Quote Link to comment Share on other sites More sharing options...
phpretard Posted January 19, 2011 Author Share Posted January 19, 2011 I am using 2 because I first need to get the set of folders to build the first menu then go into the folder and display the files as the sub menu. Does the code below clarify the question? <?php if ($handle = opendir("results/$year/Girls")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "<li><a target=\"_blank\" href=\"javascript:void(0)\">$file</a> <ul> <li><a href=\"#\">NEED FILE 1 FROM FOLDER $file</a></li> <li><a href=\"#\">NEED FILE 2 FROM FOLDER $file</a></li> <li><a href=\"#\">NEED FILE 3 FROM FOLDER $file</a></li> <li><a href=\"#\">NEED FILE 4 FROM FOLDER $file</a></li> </ul> </li>"; } }closedir($handle); } ?> Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted January 19, 2011 Share Posted January 19, 2011 oh ok, i was quickly skimming and over looked the dir handle anyway it looks good to me too. almost what I have. Try commenting out the echo with the list in it and just print $file. what comes out. Stupid check, how many files are in the /results/2011/Girls dir Quote Link to comment Share on other sites More sharing options...
phpretard Posted January 19, 2011 Author Share Posted January 19, 2011 <?php $year = date('Y'); if ($handle = opendir("results/$year/Boys")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "<li><a href=\"javascript:void(0)\">$file</a> <ul> "; if ($handle2 = opendir("results/$year/Boys/$file")) { while (false !== ($file2 = readdir($handle2))) { if ($file2 != "." && $file2 != "..") { echo "<li><a href=\"javascript:void(0)\">$file2</a>"; } } } echo "</ul></li>"; } }closedir($handle);closedir($handle2); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.