blueman378 Posted December 10, 2007 Share Posted December 10, 2007 hi guys i have this script: echo "<b>4) Select your game file</b><br>"; //Looks into the directory and returns the files, no subdirectories echo "<select name='gamefiles'>"; //The path to the style directory $dirpath = "../games_uins/"; $dh = opendir($dirpath); while (false !== ($file = readdir($dh))) { //Don't list subdirectories if (!is_dir("$dirpath/$file")) { //Truncate the file extension and capitalize the first letter echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>'; } } closedir($dh); //Close Select echo "</select>"; echo "<br><br>"; echo "<b>5) Select your thumbnail file</b><br>"; //Looks into the directory and returns the files, no subdirectories echo "<select name='thumbfiles'>"; //The path to the style directory $dirpath = "../thumbs_uins/"; $dh = opendir($dirpath); while (false !== ($file = readdir($dh))) { //Don't list subdirectories if (!is_dir("$dirpath/$file")) { //Truncate the file extension and capitalize the first letter echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>'; } } closedir($dh); //Close Select echo "</select><br><br>"; basically what it does is it reads the files in a directory, removes the extension, and then lists it in a dropdown box, my question is is there a faster way as it takes about 30-40 seconds to complete this (if not it may sut be because it is listing nearly 1000 files, thanks Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted December 10, 2007 Share Posted December 10, 2007 I dont know how much faster this is but this is the way i would do it. <?php echo(" <form method=\"POST\"> "); $dir = './pics/'; $handle = opendir($dir); while (($file = readdir($handle)) !== false) { if ( !in_array($file, array('.', '..') ) ) { $file_list[] = basename($file, '.jpg'); } } closedir($handle); asort($file_list); echo (" <center><form method=\"POST\">Select:<select name=\"pic\"> "); foreach($file_list as $key => $value){ echo (" <option>$value</option> "); } echo(" </select> <input type=\"submit\" name=\"submit\" value=\"View Pic\" /> </form></center> "); ?> Edit as needed. Quote Link to comment Share on other sites More sharing options...
btherl Posted December 10, 2007 Share Posted December 10, 2007 30-40 seconds for 1000 files seems too slow .. can you give more information about the system this is running on? Which OS? and is there any other load? You might also want to try glob() According to the user comments though, opendir() is usually faster than glob() Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 10, 2007 Author Share Posted December 10, 2007 running on home computer/ laptop win xp the laptop is not slow ( 756 ram 2.8 cpu) also this is my personal computer, so it has the normal load but even if i shut off everything else the time does not change. but i thought it was slow too, but could you give me a example of what you mean? thanks and i made a mistake before so here: echo "<b>4) Select your game file</b><br>"; //Looks into the directory and returns the files, no subdirectories echo "<select name='gamefiles'>"; //The path to the style directory $dirpath = "../games_uins/"; $dh = opendir($dirpath); while (false !== ($file = readdir($dh))) { //Don't list subdirectories if (!is_dir("$dirpath/$file")) { //Truncate the file extension and capitalize the first letter echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>'; } } closedir($dh); //Close Select echo "</select>"; Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 10, 2007 Author Share Posted December 10, 2007 how much would the preg replace slow it down? because that is helpful but not completely necessary thanks Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 10, 2007 Author Share Posted December 10, 2007 well i figured it out, the thing that was slowing it down was the if (!is_dir("$dirpath/$file")) { once i removed that it gets filled within a second, but now i have one more strange problem, now i get two blank entries in the drop down box any ideas? Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 10, 2007 Author Share Posted December 10, 2007 ah they are not blank: <option value='.'></option> <option value='..'></option> any ideas? Quote Link to comment Share on other sites More sharing options...
JacobYaYa Posted December 10, 2007 Share Posted December 10, 2007 Just do an if($file != '..' || $file != '.')){} 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.