PDA

View Full Version : PHP FileSize Retrieval? Is this correct? Because it's not working!


Golden_Eagle
11-01-2002, 11:38 AM
Please help me with the following script. I am currently using a PHP download script that is available from PHPArena. I guess you've probably heard of it!

Is it possible for PHP to find out the FileSize of the file from the 'ID' it has registered in my MySQL server database without me having to enter the lot into a seperate table?

This is something I have found that seems to do something with the filesize. I think it looks for it - then converts it to GB, MB, KB or bytes. But I've installed and tried it. But it will not work! :(

<?
$file = $pafiledb_sql->query($db, "SELECT * FROM pafiledb_files WHERE file_id = '$id'", 1);
$time = time();
$update = $pafiledb_sql->query($db, "UPDATE pafiledb_files SET file_dls=file_dls+1, file_last=$time WHERE file_id = '$id'", 0);
?>
<?
$file_size = filesize(".".$file[file_dlurl]);
if($file_size >= 1073741824)
{
$file_size = round($file_size / 1073741824 * 100) / 100 . "Gb";
}
elseif($file_size >= 1048576)
{
$file_size = round($file_size / 1048576 * 100) / 100 . "Mb";
}
elseif($file_size >= 1024)
{
$file_size = round($file_size / 1024 * 100) / 100 . "Kb";
}
else{
$file_size = $file_size . "Bytes";
}
?>

<center>
<br><br>
You are downloading file <?=$file[file_name]?>. File size is <b><?=$file_size?></b> <br>
To download file <a class=menu href="<?=$file[file_dlurl]?>">click here</a>.

Is this line trying to work out the filesize?
$file_size = filesize(".".$file[file_dlurl]);

All other $File statements are included in my database.

If I'm totally wrong please let me know how this can be changed or replaced to do what I need as adding 12,600 file sizes to my database will probably take a lifetime!

Thanks in advance
Golden Eagle.
Webmaster of Font-Factory.com

mordred
11-01-2002, 01:58 PM
You are right, this is the necessary part to find out the size of a file. However, you have to be more precise as to what exactly does not work. Two things I can think of:

$file_size = filesize(".".$file[file_dlurl]);

What's the value of $file[file_dlurl]? It could be a relative path, an absolute path, an URL, or something else. Only you know how the file paths are stored in your db. If you only have it like "filex.ttf", then you most certainly won't get the file size by the code above.

So knowing what's in $file[file_dlurl] is vital for your application.

Second thing: Does your query() method return the result set automatically as an array or as a resource? I ask this because in the latter case you'd have to employ mysql_fetch_xxx() functions to retrieve the result set's values.

Golden_Eagle
11-01-2002, 02:59 PM
The $file[file_dlurl] command is pointing to a URL of a .zip with this format - ../downloads/file.zip

As for the Second thing. I have absolutely no idea what you mean! :(

Maybe if I gave a little more information on the script I'm using. The actual script is from PHPArena.net and is called PHPFileDB 3. The site has a forum and somebody posted this as a mod for the PHP Program. But the forum seems to be somewhat redundant. Unlike the ever popular, ever ready codingforums.com!

The script above is in it's own file (download.php). If that helps..?

I have completely copied the above script from the forum it was posted on. So I don't think the 'Second Thing' is actually needed. Although I could be wrong. ( I'm relatively new-ish to PHP ) :D

In this 'string' it says

$file_size = filesize(".".$file[file_dlurl]);

Is the ",", bit actually neccessary? Or is this needed?

Ökii
11-01-2002, 05:07 PM
To my understanding, the "." is well dodgy - unless you wanted to find the filesize of a .htaccess by only typing htaccess.

Anyway, onward....

I would suggest that rather than using the ID from the mysql table and letting PHP sniff the file and return its size - you should consider actually adding another field to the database table and store the info there.
So - rather than giving php (and your server) a workout at every page load, you are simply pulling one more field of information to the page than previously.

Next...

You say it is called download.php - so can I assume that your visitors are sent to a page that has content in order to download the files? The 'you are downloading....' echo seems to indict so.
I would in this case suggest that you change tactics slightly and use a download script that doesn't alter the page (basically you have your page with a.ttf b.ttf c.ttf etc and download buttons - clicking one of those buttons simply activates a script that does not affect the browser at all [ie the page doesn't change, they just get the 'save to..' confirmation and the download is complete])
Using a script that doesn't effect the page where they clicked the [download this font] button also reduces your bandwidth marginally as the page isn't refreshed.
You could easily update a mysql table within the download script as a means of counting how many times a font had been downloaded.
You could also then just have the font filesize displayed on the same page as the download button and would probably angle toward storing that filesize within the db.

Golden_Eagle
11-01-2002, 05:22 PM
That sounds all well and good written down now. But if I embark on adding the FileSize to every font in my database. I could be doing it till I'm old and grey!

You see having over 12,600 fonts is gonna take a hell of a long time to add to my database.

As, at the moment, I'm only using the PHPMyAdmin script to alter it!

I know you can download the code for the database. But, I have'nt found a decent offline editor for the layout of my dumped file!

Also downloading the file always takes quite a while! (No Broadband) And the file that comes from MySQL Server is over 10Mb....... For just a single .TXT file!

As for Bandwidth. I don't think I'll be having too many problems with that just yet. Not at least until my site becomes popular. It's currently only receiving 500 odd uniques per day.

mordred
11-01-2002, 11:45 PM
I have to agree with what Okii said. The dot in the filesize() function seems to be the culprit. Try to remove it at all and report if anything changes.

If not, try to echo a HTML link and test that to see if you correctly link to the desired file. You can echo a link by using


echo "<a href='" . $file['file_dlurl'] . "'>file</a>";


and for comparing, do the same with the dot appended:


echo "<a href='" . "." . $file['file_dlurl'] . "'>file</a>";

Ökii
11-02-2002, 11:55 AM
You wouldn't update the new field in the table manually - you'd just run a script that goes through all the font files in a defined folder and automatically adds the filesize to the database.

As a subnote: I went to your site (I use 1600x1200 res btw) and left immediately the window resized - and imo going fullscreen when your content area is designed for 800x600 or below just makes the site look worse.