I would like some feedback on my code.
Just to be clear, it works perfectly. But what I am interested in knowing is what can be done
to make this segment of my site faster and more efficient..
The idea is simple.. have PHP go into a directory that contains full sized images and small companion thumbnails.
Sort them such that all thumbnail filenames are put into one array while the full sized filenames are
put into another array.
So inside this directory: illustrations/images/
I have:
illust01_thumbv2-0.gif
illust01v2-0.jpg
illust02_thumbv2-0.gif
illust02v2-0.jpg
illust03_thumbv2-0.gif
illust03v2-0.jpg
, etc...
Here is my code:
$dir = $_SERVER['DOCUMENT_ROOT'].'/illustrations/images/';
if(is_dir($dir)){ // test to see if folder exists
$fullImageArray = array();
$thumbnailArray = array();
$thumbnailTotal = 0;
$fullImageTotal = 0;
$scanarray = scandir($dir);
for($i=0; $i < count ($scanarray); $i++){
if($scanarray[$i]!="." && $scanarray[$i]!=".."){
if(is_file($dir . $scanarray[$i])){ // find out if this is a file
if(ereg('_thumb', $scanarray[$i])){ // find only the thumbnail images
$thumbnailArray[$thumbnailTotal] = $scanarray[$i];
$thumbnailTotal++;
} else {
$fullImageArray[$fullImageTotal] = $scanarray[$i];
$fullImageTotal++;
}
}
}
}
} else {
echo "<p class=\"errorFont\">Error! Directory does not exist!</p>";
}
What can be done to improve on this? How can I reduce code overhead and speed-up / streamline this process?
Any suggestions would be appreciated.
Cheers,
NRG
*Edit: I am using PHP 5.2.6 (in case of suggested functions that require the latest version).