Closed Thread Icon

Preserved Topic: Quick little question on PHP Pages that link to <a href="https://ozoneasylum.com/backlink?for=21203" title="Pages that link to Preserved Topic: Quick little question on PHP" rel="nofollow" >Preserved Topic: Quick little question on PHP\

 
Author Thread
junkyboy
Nervous Wreck (II) Inmate

From: Under, The, Sea
Insane since: Jun 2001

posted posted 10-25-2001 18:53

I've got this small code snippet that I got off the web, and was wondering if I could implement something that could find the size of a folder (not including subfolders), and print it out on screen. I found another little snippet on the PHP web page:

function dirsize($dir) {
$dh = opendir($dir);
$size = 0;
while (($file = readdir($dh)) !== false)
if ($file != "." and $file != "..") {
$path = $dir."/".$file;
if (is_dir($path))
$size += dirsize($path);
elseif (is_file($path))
$size += filesize($path);
}
closedir($dh);
return $size;
}

The only problem is, a) I KIND of understand what's going on, but just don't know if my "revised" one would work or not, and b) I was wondering if the return value of filesize(folder) would just be the answer to my question.

The following is my "revised" version:

$dh = opendir($dir);
$size = 0;
while (($file = readdir($dh)) != false) {
if ($file != "." and $file != "..") {
$path = $dir . "/" . $file;
if (is_file($path))
$size += filesize($path);
}
}
closedir($dh);

I think it works, I'm not 100% sure, though. Would filesize(folder) do the exact same thing as what I have above? Any better way of doing this?

junkyboy
Nervous Wreck (II) Inmate

From: Under, The, Sea
Insane since: Jun 2001

posted posted 10-25-2001 19:23

Nope, it actually doesn't work..

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 10-25-2001 22:21

This will do what you want:

<?php

// Written by mr.maX, http://www.maxworld.co.yu/

function dirsize($dir) {
&nbsp;&nbsp;&nbsp;&nbsp;$dh = opendir($dir);
&nbsp;&nbsp;&nbsp;&nbsp;$size = 0;
&nbsp;&nbsp;&nbsp;&nbsp;while ($file = readdir($dh)) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$path = "$dir/$file";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (is_file($path)) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$size += filesize($path);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;closedir($dh);
&nbsp;&nbsp;&nbsp;&nbsp;return $size;
}

print dirsize(".");

?>


junkyboy
Nervous Wreck (II) Inmate

From: Under, The, Sea
Insane since: Jun 2001

posted posted 10-26-2001 01:50

I'm not sure I'm doing this right.. What would the parameter $dir look like? If it's a handle to the path, would it mean I would have to do a call to opendir()?

junkyboy
Nervous Wreck (II) Inmate

From: Under, The, Sea
Insane since: Jun 2001

posted posted 10-26-2001 01:52

Oops, I meant a call to dirname()?

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 10-26-2001 01:54

You would need to set it in your script.

Something like

$dir = "your_directory";

junkyboy
Nervous Wreck (II) Inmate

From: Under, The, Sea
Insane since: Jun 2001

posted posted 10-26-2001 02:17

Hmm, I've got it working (mostly), except it only checks the first file's size and prints it out :P It seems like it's breaking out of the while() a bit too fast..

junkyboy
Nervous Wreck (II) Inmate

From: Under, The, Sea
Insane since: Jun 2001

posted posted 10-26-2001 02:54

OK, I think this is what's going on.. It's somehow reading "." as a file, and.. it seems to forget the rest of the files.. Any clues?

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 10-26-2001 07:12

I have tested my function on Windows and Linux and it works without any problems. And as in my example simply call it like this:

print dirsize("/path/to/your/directory");

print dirsize("c:/www/html");

BTW1 There must not be a trailing slash at the end of directory that's specified in dirsize() function call.

BTW2 In my example it will output the size of the directory that it runs from (that's what "." stands for).

BTW3 If you still can't make it to work, post your complete script here so that I can analyze it.


« BackwardsOnwards »

Show Forum Drop Down Menu