hello everyone
well, i'm going to say straight up i am very much a novice at php and all that i've learned has been gathered mostly by analyzing other people's work. one ongoing project i've been working on is a simple file upload page that lists the files and sorts them alphabetically. my goal is to be able to let people also sort them by filesize and date uploaded. i have been told that this is almost impossible without using mysql - is that true?
anyway, here is the page: http://www.thebackalleys.com/ok/public/ WARNING: it takes about a minute for the page to load (therein lies the problem).
and here is the script on the page, which is a mixture of code that i wrote along with code that i have found and modified to suit my needs (mostly for the look of the page):
<?
$colorbar = 0;
$path = ".";
$dir_handle = opendir($path) or die("Unable to open $path");
$paths = array();
while ($file = readdir($dir_handle)){
if(!is_dir($file)){
$paths[] = "$path$file";
}
}
closedir($dir_handle);
sort($paths);
foreach ($paths as $link) {
$show = str_replace(" ","%20",substr($link,strrpos($link, '/')+1));
if ($show != ".htaccess" && $show != ".ftpquota" && substr($show,-3) != "php" && $show != "ajax-loader.gif"){
$total_files++;
$root = "http://www.thebackalleys.com/ok/public/";
$root .= $show;
$root = str_replace(" ","%20",$root);
if ($colorbar&1){
echo "</tr><tr><td align='left'><a href=" . $show . ">";
}else{
echo "</tr><tr><td align='left' bgcolor='#343234'><a href=" . $show . ">";
}
$show = substr($link,strrpos($link, '/')+1);
echo "$show</a></td>\n";
$f = fopen($root, 'r');
if ($colorbar&1){
echo "<td align='right'>";
$colorbar++;
}else{
echo "<td align='right' bgcolor='#343234'>";
$colorbar++;
}
$divsize = sprintf("%01.2f",filesize($show)/1000,-1);
$total_filesize = $total_filesize + $divsize;
if ($divsize < 1100){
echo " " . $divsize;
echo "KB";
}else{
echo " " . sprintf("%01.1f",$divsize/1000,-1);
echo "MB";
}
echo "</td>";
fclose($f);
}else{
}
}
echo "</table>";
echo "<p>\n";
echo "Total files: <b>$total_files</b> <br>\n";
$average_filesize = sprintf("%01.1f",$total_filesize/$total_files,1);
echo "Average file size: <b>$average_filesize</b>KB <br> \n";
$total_filesize = sprintf("%01.1f",$total_filesize/1000,1);
echo "Total directory size: <b>$total_filesize</b>MB <br>\n";
echo "</p>";
?>
i know the loops is what is making this slow. i guess i have 3 questions:
1. is this script poorly written, and if so how can i optimize it? however,
2. if it can't be made efficient in any possible way, does it need to be made in mysql?
and if that is the case, 3. can anyone point me in the right direction on how to begin writing such a script?
thank you for your time,
-rtil