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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.