iamcaper Posted April 30, 2007 Share Posted April 30, 2007 Hey Gang, Thanks to those of you who've tried to help so far. I have the following code that populates a drop down with a directory list of files of a specified type: <?php //Images to ignore, File name ONLY, no extension, seperate by a comma, no spaces //Example: $ignoreimages = "BlahImage,ImageMeh"; $ignoreimages = ""; $direc = "path"; $dh = opendir($direc); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } $output .= "<form name='FileList' method='post' action=''> <select name='select'>"; //Accepted File Types To display $acceptedfiles = array(1 => "jpg", 2 => "png", 3 => "gif", 4 => "tif"); //Build the array that holds the files to exclude. $ignore = explode(",", $ignoreimages); $FileCount = count($files); for($i=0; $i <= $FileCount; $i++){ $file1 = explode(".", $files[$i]); $blah = count(file1); $extension = $file1[$blah]; $filename = $file1[$blah -1]; if(in_array($extension, $acceptedfiles)) { if(!in_array($filename, $ignore)){ $output .= "<option value='". $files[$i] ."'>". $files[$i] ."</option>"; } } } //Close the file out $output .= "</select> </form>"; //output the HTML echo $output; ?> I'd like to expand this so that it's a chained select which is automatically populated by directory contents. For example, the main directory could be: option1 option2 option3 If you select "option2" it will give you options in the next drop down menu, which has been populated by a folder within the "option2" folder. Does this make sense or am I not explaining it properly? Eventually, I want to be able to have the selected file (within the final drop down) open in the interface I'm developing. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 30, 2007 Share Posted April 30, 2007 Yes, it makes sense. But, you need to make a decision. Either 1) You need to have the page refresh every time the user changes the folder so the page can query the contents of that folder or 2) You need to query all of the possible folders and get all of the possible files. then dump the results into a javascript array. Then you could have the contents of the 2nd select list change dynamically w/o a page refresh. Well, you could also use AJAX to repoulate the select list w/o a refresh. But, I have found problems with these types of implementations due to a slight delay. Quote Link to comment Share on other sites More sharing options...
iamcaper Posted April 30, 2007 Author Share Posted April 30, 2007 I guess that considering the sheer amount of files that I'll potentially be faced with, it would be best to refresh. The size would probably be better handled by a database but I want to do it this way because the files will be constantly changin. So, to make a decision, I'll go with 1 and do a refresh. Quote Link to comment Share on other sites More sharing options...
iamcaper Posted May 1, 2007 Author Share Posted May 1, 2007 Can anybody else step in here to offer assistance? I'd like to have drop down's, the same as in Xin Yang's Chained Select, but I would like for the menu's to be automatically filled by file lists from specified directories. If you would like more info on what I'm trying to accomplish please feel free to message me. Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 1, 2007 Share Posted May 1, 2007 Here is a working example. Just change the $parent_directory value to the relative path where the folders reside. <?php $parent_directory = '.'; $file_types = 'jpg,png,gif,tif'; //===================================================// // FUNCTION: directoryToArray // // // // Parameters: // // - $root: The directory to process // // - $to_return: f=files, d=directories, b=both // // - $file_types: the extensions of file types to // // to return if files selected // //===================================================// function directoryToArray($root, $to_return='b', $file_types=false) { $array_items = array(); if ($file_types) { $file_types=explode(',',$file_types); } if ($handle = opendir($root)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $add_item = false; $type = (is_dir($root. "/" . $file))?'d':'f'; $name = preg_replace("/\/\//si", "/", $file); if ($type=='d' && ($to_return=='b' || $to_return=='d') ) { $add_item = true; } if ($type=='f' && ($to_return=='b' || $to_return=='f') ) { $ext = end(explode('.',$name)); if ( !$file_types || in_array($ext, $file_types) ) { $add_item = true; } } if ($add_item) { $array_items[] = array ( 'name'=>$name, 'type'=>$type, 'root'=>$root); } } } // End While closedir($handle); } // End If return $array_items; } if (isset($_POST[pickfile])) { // User has selected a file take whatever action you want based // upon the values for folder and file } else { echo ' <html> <head> <script type="text/javascript"> function changeFolder(folder) { document.pickFile.submit(); } </script> </head> <body>'; echo "<form name=\"pickFile\" method=\"POST\">\n"; $directoryList = directoryToArray($parent_directory,'d'); echo "<select name=\"folder\" onchange=\"changeFolder(this.value);\">\n"; foreach ($directoryList as $folder) { $selected = ($_POST[folder]==$folder[name])? 'selected' : ''; echo "<option value=\"$folder[name]\" $selected>$folder[name]</option>\n"; } echo '</select><br><br>'; $working_folder = ($_POST[folder]) ? $_POST[folder] : $directoryList[0][name]; $fileList = directoryToArray($parent_directory.'/'.$working_folder,'f',$file_types); echo "<select name=\"file\">\n"; foreach ($fileList as $file) { echo "<option value=\"$file[name]\">$file[name]</option>\n"; } echo '</select><br><br>'; echo "<button type=\"submit\" name=\"pickfile\">Submit</button>\n"; echo "</form>\n"; echo "</body>\n"; echo "</html>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
iamcaper Posted May 1, 2007 Author Share Posted May 1, 2007 Now I'm on the right track, thanks mjdomato! Now I just have to figure out how to get subsequent folders/files displayed in additional menu's, all based on what you select in the previous menu. hehe You rock! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 1, 2007 Share Posted May 1, 2007 Please mark the thread as solved. 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.