View Full Version : Resolved help with file size with php
ajfmrf
02-19-2014, 06:59 AM
I have a working directoery lister that I was able to adapt some code to but I am getting frustrated in a atempt to get the file sizes to be more precise.
Php is not my strongest thing-lol
this is used in a file to get file sizes:
$size=number_format(filesize($dirArray[$index]));
I found this code but the array in the first example is messing me up.
function getFilesize($file,$digits = 2) {
if (is_file($file)) {
$filePath = $file;
if (!realpath($filePath)) {
$filePath = $_SERVER["DOCUMENT_ROOT"].$filePath;
}
$fileSize = filesize($filePath);
$sizes = array("TB","GB","MB","KB","B");
$total = count($sizes);
while ($total-- && $fileSize > 1024) {
$fileSize /= 1024;
}
return round($fileSize, $digits)." ".$sizes[$total];
}
return false;
}
I tried first to remove the red highlighted part that did not work.
I tried this too and it did not work
$fileSize = filesize($filePath);
$sizes = array("TB","GB","MB","KB","B");
$total = count($sizes);
while ($total-- && $fileSize > 1024) {
$fileSize /= 1024;
}
return round($fileSize, $digits,$dirArray[$index])." ".$sizes[$total];
}
return false;
}
Any hint where I should be going with this?
the original script shows only byte size.which can be large for some stuff-lol
If needed,I can put up a link to a text file of the working script.
thanks in advance
I have a working directoery lister that I was able to adapt some code to but I am getting frustrated in a atempt to get the file sizes to be more precise.
What do you mean by "more precise"? filesize (http://php.netfilesize) is as accurate as you'll get. If you're getting incorrect results, it may be a caching issue: try calling clearstatcache (http://php.net/clearstatcache) first. Another possible issue would be trying to check files over 2GB on a 32-bit system, but this is unlikely.
function getFilesize($file,$digits = 2) {
if (is_file($file)) {
$filePath = $file;
if (!realpath($filePath)) {
$filePath = $_SERVER["DOCUMENT_ROOT"].$filePath;
}
$fileSize = filesize($filePath);
$sizes = array("TB","GB","MB","KB","B");
$total = count($sizes);
while ($total-- && $fileSize > 1024) {
$fileSize /= 1024;
}
return round($fileSize, $digits)." ".$sizes[$total];
}
return false;
}
I tried first to remove the red highlighted part that did not work.
What do you mean by "did not work"? What are you trying to accomplish? This function works as expected for me: it returns the filesize in (as appropriate) Bytes, KB, MB, GB, TB.
I tried this too and it did not work
$fileSize = filesize($filePath);
$sizes = array("TB","GB","MB","KB","B");
$total = count($sizes);
while ($total-- && $fileSize > 1024) {
$fileSize /= 1024;
}
return round($fileSize, $digits,$dirArray[$index])." ".$sizes[$total];
}
return false;
}
That's not even syntactically correct - it would cause a fatal error. Do you use a code editor? It would be able to catch errors like this. Even something with simple syntax highlighting would go a long way towards helping you catch syntax problems.
ajfmrf
02-19-2014, 06:01 PM
Okay Adrian here is the whole file I have
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Directory Contents</title>
<link rel="stylesheet" href=".style2.css">
<script src=".sorttable.js"></script>
</head>
<body>
<div id="container">
<h1>Directory Contents</h1>
<table class="sortable">
<thead>
<tr>
<th>Filename</th>
<th>Type</th>
<th>Size <small>(bytes)</small></th>
<th>Date Modified</th>
</tr>
</thead>
<tbody>
<?php
// Opens directory
$myDirectory=opendir(".");
// Gets each entry
while($entryName=readdir($myDirectory)) {
$dirArray[]=$entryName;
}
// Finds extensions of files
function findexts ($filename) {
$filename=strtolower($filename);
$exts=split("[/\\.]", $filename);
$n=count($exts)-1;
$exts=$exts[$n];
return $exts;
}
// Closes directory
closedir($myDirectory);
// Counts elements in array
$indexCount=count($dirArray);
// Sorts files
sort($dirArray);
// Loops through the array of files
for($index=0; $index < $indexCount; $index++) {
// Allows ./?hidden to show hidden files
if($_SERVER['QUERY_STRING']=="hidden")
{$hide="";
$ahref="./";
$atext="Hide";}
else
{$hide=".";
$ahref="./?hidden";
$atext="Show";}
if(substr("$dirArray[$index]", 0, 1) != $hide) {
// Gets File Names
$name=$dirArray[$index];
$namehref=$dirArray[$index];
// Gets Extensions
$extn=findexts($dirArray[$index]);
// Gets file size
$size=number_format(filesize($dirArray[$index]));
// Gets Date Modified Data
$modtime=date("M j Y g:i A", filemtime($dirArray[$index]));
$timekey=date("YmdHis", filemtime($dirArray[$index]));
// Prettifies File Types, add more to suit your needs.
switch ($extn){
case "png": $extn="PNG Image"; break;
case "jpg": $extn="JPEG Image"; break;
case "svg": $extn="SVG Image"; break;
case "images/gifgif": $extn="GIF Image"; break;
case "ico": $extn="Windows Icon"; break;
case "txt": $extn="Text File"; break;
case "log": $extn="Log File"; break;
case "htm": $extn="HTML File"; break;
case "php": $extn="PHP Script"; break;
case "js": $extn="Javascript"; break;
case "css": $extn="Stylesheet"; break;
case "pdf": $extn="PDF Document"; break;
case "zip": $extn="ZIP Archive"; break;
case "bak": $extn="Backup File"; break;
default: $extn=strtoupper($extn)." File"; break;
}
// Separates directories
if(is_dir($dirArray[$index])) {
$extn="<Directory>";
$size="<Directory>";
$class="dir";
} else {
$class="file";
}
// Cleans up . and .. directories
if($name=="."){$name=". (Current Directory)"; $extn="<System Dir>";}
if($name==".."){$name=".. (Parent Directory)"; $extn="<System Dir>";}
// Print 'em
print("
<tr class='$class'>
<td><a href='./$namehref'>$name</a></td>
<td><a href='./$namehref'>$extn</a></td>
<td><a href='./$namehref'>$size</a></td>
<td sorttable_customkey='$timekey'><a href='./$namehref'>$modtime</a></td>
</tr>");
}
}
?>
</tbody>
</table>
<h2><?php print("<a href='$ahref'>$atext hidden files</a>"); ?></h2>
</div>
</body>
</html>
this results in the file size being posted the long way
examples are 65,049bytes,496,928bytes
I am trying to get that to put it into rounded kb,mb,gb or tb if it is of one type size or the other.
I am probally not wording this correctly.
I tried to change the yellow part to make this change and is is not working.I get errors and have tried many ways-too many to list here -to work this out.
I tried to get the "file size" part to be yellow but it is not working???
this results in the file size being posted the long way
examples are 65,049bytes,496,928bytes
I am trying to get that to put it into rounded kb,mb,gb or tb if it is of one type size or the other.
The getFilesize function from your original post does this—you're not using it here. Have you tried it? Something like
<?php
// . . .
$size = getFilesize( $dirArray[$index] );
// . . .
I get errors and have tried many ways-too many to list here -to work this out.
Well, just start with the one that seemed "most" successful, and the first (or first few) errors. It's always helpful.
I tried to get the "file size" part to be yellow but it is not working???
If you're talking about the BBCode tags, they don't work inside [PHP] or [HTML] tags (though they do work inside plain ol' [CODE] tags).
ajfmrf
02-19-2014, 10:24 PM
Hi Adrian,that original part will give the file size to each file.
However I was trying to get it to work differently.
As it is originally it will give size s bytes only,even for those that are fairly big.
like:
60,593bytes or 396,659bytes
I am trying to have it round to kb,mb if the size warrants it.
I know it works fine giving file size I am just trying to get it to go to the next level so to speak.
Am I confusing things-upgrade this to use kb,mb instead of just bytes ....
Hi Adrian,that original part will give the file size to each file.
However I was trying to get it to work differently.
As it is originally it will give size s bytes only,even for those that are fairly big.
like:
60,593bytes or 396,659bytes
I am trying to have it round to kb,mb if the size warrants it.
I know it works fine giving file size I am just trying to get it to go to the next level so to speak.
Am I confusing things-upgrade this to use kb,mb instead of just bytes ....
Are we both talking about this function?
function getFilesize($file,$digits = 2) {
if (is_file($file)) {
$filePath = $file;
if (!realpath($filePath)) {
$filePath = $_SERVER["DOCUMENT_ROOT"].$filePath;
}
$fileSize = filesize($filePath);
$sizes = array("TB","GB","MB","KB","B");
$total = count($sizes);
while ($total-- && $fileSize > 1024) {
$fileSize /= 1024;
}
return round($fileSize, $digits)." ".$sizes[$total];
}
return false;
}
Now, there are some things about it that I don't like, but when I test this function, it does return the file size in KB/MB/etc., whichever is most natural to the file size. Does it not do so for you? Can you provide an example where it fails to do so?
ajfmrf
02-20-2014, 10:57 AM
Adrian.my problem is ,I don't know how to rewrite the function .
Yes,we are talking about the same "new" function.
I take the original one and tried to redo it many different ways and so far nothing has given me a working version.
Each on returns errors.(I have tried many ways -too many to post -they don,t work)
the ($dirArray[$index] I think is what has me confused ????
Adrian.my problem is ,I don't know how to rewrite the function .
Yes,we are talking about the same "new" function.
I take the original one and tried to redo it many different ways and so far nothing has given me a working version.
Alright- what I'm saying is, the getFilesize function (as written above, with no changes) works exactly as you describe when I test it.
Each on returns errors.(I have tried many ways -too many to post -they don,t work)
You need to share the errors you are getting. I cannot help you by guessing. If there are "too many to post," start with the first few.
I don't mean to be blunt, but there is no point in asking for help at all if you don't provide specific information about what is going wrong.
the ($dirArray[$index] I think is what has me confused ????
What is confusing you about it? Have you checked (e.g., using var_dump (http://php.net/var_dump)) to make sure the variable and index contain the value you expect?
ajfmrf
02-21-2014, 03:20 AM
okay-forget it.
okay-forget it.
??
I'm not trying to discourage you, I'm just asking you to tell us what the specific problem is. If you do so, I (or someone) can probably help you find a solution.
ajfmrf
02-21-2014, 12:31 PM
I don't know how many times or ways I can say it.
The script returns all file sizes in bytes-regardless as t hoe big it is.
I was merely hoping to get the script to change and give results in kb,mb,gb,tb if the size was this big
As it is it reports sizes in bytes only
ie; 397bytes,4,932bytes 34,987bytes or even 234,875bytes and so on in stead of 3.2kb or 1.4mb and so on.
I will keep tinkering with the original until I either get it or decide to dump the idea.
I just thought that if I posted the original script and the second one(this uses the bytes,kb,mb,gb,tb in the script) that it would be an easy fix for a php guru.
But thanks for looking
ajfmrf
02-21-2014, 02:10 PM
As to my request at http://css-tricks.com a new file was made and it includes the re-work for file sizes like I was looking for.
with that and my addition of the image atributes for width and height I am a happy camper now.
here is my working file
http://www.web-user.info/directory/.index.php
I will submit this as resolved
I don't know how many times or ways I can say it.
The script returns all file sizes in bytes-regardless as t hoe big it is.
I was merely hoping to get the script to change and give results in kb,mb,gb,tb if the size was this big
As it is it reports sizes in bytes only
ie; 397bytes,4,932bytes 34,987bytes or even 234,875bytes and so on in stead of 3.2kb or 1.4mb and so on.
I understand this. The thing I was trying to understand is why you are getting "bytes only" results.
This is confusing to me, because I tested the function several times, and the result was not displayed in "bytes only"; it correctly used "KB" or "MB", etc..
When you say the script "returns errors", do you mean that there are actual error messages (i.e., PHP errors)? or just that it gave you a result you did not want? Perhaps that was part of our confusion.
I just thought that if I posted the original script and the second one(this uses the bytes,kb,mb,gb,tb in the script) that it would be an easy fix for a php guru.
But thanks for looking
You are welcome. I understand that you have resolved this problem, but, if you are willing, could you post the code that is working as you desired? I'd be interested in understanding what the difference might be.
ajfmrf
02-22-2014, 02:35 AM
Hi Adrian these are the files(text files) of the original script
http://www.web-user.info/directory/.aindex.txt
And here is the new version
http://www.web-user.info/directory/.index.txt
I was able to add the attributes part to it and get it to show the width and height of images-there probally is a better way to do it then what I came up withThis is my first success getting php code to work on my own :)
Maybe a new way to change it so just the numbers without the "width" and the "height" would be an upgrade to what I got to work but being a newbie I am happy with the way it is , lol
Notice the second file is larger and includes to code to list file sizes like we were looking at with the new code I found.
The code is located here:
http://css-tricks.com/snippets/php/display-styled-directory-contents/
What is embeded at the top of the page is the old script-go way down to the end of that code and get the zip file which has the updated files
any questions? it is a neat script I think.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.