Jump to content

a faster way?


blueman378

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/80975-a-faster-way/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/80975-a-faster-way/#findComment-410808
Share on other sites

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()

Link to comment
https://forums.phpfreaks.com/topic/80975-a-faster-way/#findComment-410812
Share on other sites

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>";

Link to comment
https://forums.phpfreaks.com/topic/80975-a-faster-way/#findComment-410818
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/80975-a-faster-way/#findComment-410876
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.