Jump to content

file upload/sort script - runs terribly slow!


rtil

Recommended Posts

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

Link to comment
Share on other sites

it looks like every time the page loads you are parsing the files that are in the local directory "."

 

if this is an upload script, why not use a SQL database and just keep track of what files were uploaded.  then when you want to display them just run a simple query on the database which is MUCH faster than parsing the filesystem every time

Link to comment
Share on other sites

it looks like every time the page loads you are parsing the files that are in the local directory "."

 

if this is an upload script, why not use a SQL database and just keep track of what files were uploaded.  then when you want to display them just run a simple query on the database which is MUCH faster than parsing the filesystem every time

i'd love to do that, but as i previously stated, i have no idea how to go about writing a script like that and i need a little guidance.

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.