Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    Somewhere on earth. Hmm...
    Posts
    115
    Rep Power
    19

    Get directory size with PHP efficiently


    Hey,

    I have been looking for a script that will return the size of a directory that works fast. I have found many scipts that return the size of a given directory, but the problem is that the script scans through all the files, gets their size, adds to $totalsize and then echos $totalsize. This may sounds greate, except when it comes to directories containing many files, such as 50,000+, where you get the max execution time exceeded.

    I tried using disk_total_size("C:/Directory/") but it returns the total size of the partition C: which is not what i need. I also gave filesize("C:/Directory/") a shot but it just returns a zero.

    Anyone have a script that will return the size of a directory containing many files fast?

    Thanks
  2. #2
  3. Puppy Kicker
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Texas
    Posts
    323
    Rep Power
    52
    filesize() is the quickest way...I assume those scripts use it. There's no PHP function to get the size of a folder, so you have to make do by adding up the individual contents of the folder.
    If I had a dollar for every time I've said something stupid, I might be able to afford an education.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    Somewhere on earth. Hmm...
    Posts
    115
    Rep Power
    19
    Well if you have many files (such as 50,000+), it tends to take long, sometimes exceeding the max script execution time. I guess if i look into making my own script, i could probably tweak it to my needs.

    Im surprised PHP doesn't have a function to do such thing.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2005
    Posts
    1,579
    Rep Power
    277
    Windows you can use COM, it's much more faster than trying to script something. Unix/Linux popen works great!


    Windows.... PHP via (COM)

    Code:
    <?
    	$f = 'f:/www/docs';
    
    	$obj = new COM ( 'scripting.filesystemobject' );
    
    	if ( is_object ( $obj ) )
    	{
    		$ref = $obj->getfolder ( $f );
    
    		echo 'Directory: ' . $f . ' => Size: ' . $ref->size;
    
    		$obj = null;
    	}
    	else
    	{
    		echo 'can not create object';
    	}
    
    ?>
    Linux one way...

    Code:
    <?
    
    	$f = './path/directory';
    
    	$io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
    
    	$size = fgets ( $io, 4096);
    
    	$size = substr ( $size, 0, strpos ( $size, ' ' ) );
    
    	pclose ( $io );
    
    	echo 'Directory: ' . $f . ' => Size: ' . $size;
    
    ?>

    printf
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    Somewhere on earth. Hmm...
    Posts
    115
    Rep Power
    19
    Thanks man. This is definately much quicker than any script.

    Benchmark for windows:
    File: 36,133 Folders: 6,210
    Time taken to return total size: 19.614099 seconds
    Script output: Directory: K:/Windows => Size: 7506880321
    Computer Configs:
    CPU: Intel 3 GHz
    RAM: 512MB
    Hard-disk: 80GB SATA 7,200RPM Seagate

    I will try the linux version when i move to the host. Im assuming it will be faster since the server will be dedicated to hosting only.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    Somewhere on earth. Hmm...
    Posts
    115
    Rep Power
    19
    Hey,

    I moved to my new host (godaddy) a couple of days ago. Took a while to get things up and running. Anyway, i tried the linux version but it didnt return the size.

    Here is the adderss to my script: http://www.myimagehosted.com/dir_size.php

    Just to be sure, this is how my path starts of "/home/content/..."
  12. #7
  13. Retired
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Finland
    Posts
    9,112
    Rep Power
    2495
    Turn error_reporting up to the max, or if you have access to you log files -
    do you get any errors?

    Cheers,
    Jamie

    >_ My Music Blog | Losing weight @notsoheavyblog | My Tweets

    __________________

    Let the might of your compassion arise to bring a quick end
    to the flowing stream of the blood and tears .....
    Please hear my anguished words of truth.

    __________________
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    Somewhere on earth. Hmm...
    Posts
    115
    Rep Power
    19
    No errors.

    PHP Code:
    <?PHP

    error_reporting
    (E_ALL);

    $f '/home/content/h/a/m/hamoodyjamal/html';

    $io popen '/usr/bin/du -sk ' $f'r' );

    $size fgets $io4096);

    $size substr $size0strpos $size' ' ) );

    pclose $io );

    echo 
    'Directory: ' $f ' => Size: ' $size;

    ?>
    I dont have access to the log files. It is shared hosting.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Posts
    778
    Rep Power
    239
    In addition to
    PHP Code:
    error_reporting(E_ALL); 
    I would also try
    PHP Code:
    ini_set('display_errors''1'); 
  18. #10
  19. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,939
    Rep Power
    900
    There is no error to be had, I don't think.. What error should be happening ?

    [edit]
    Let me clarify why I say that. popen() returns a handle even on failure, so the script shouldn't mess up anywhere, it just won't do what it's expected to.
    [/edit]
    Last edited by angrypanda; July 11th, 2006 at 06:05 PM.
    Verify and sanitize ALL OUTSIDE DATA.
  20. #11
  21. doesn't like link-rollover ads
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2003
    Posts
    1,818
    Rep Power
    152
    dude... you were so close to begin with... [phpnet=disk_total_space]disk_total_space()[/phpnet]
  22. #12
  23. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,939
    Rep Power
    900
    That however will not return disk space used but size of files.

    [edit]

    If that is an issue or not I don't know, can't tell from OP.

    [/edit]
    Last edited by angrypanda; July 11th, 2006 at 07:32 PM.
    Verify and sanitize ALL OUTSIDE DATA.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    Somewhere on earth. Hmm...
    Posts
    115
    Rep Power
    19
    disk_total_space did not work. It returned something like "3.77583258829E+12"

    ini_set('display_errors', '1'); also didnt do anything. No errors reported.
  26. #14
  27. No Profile Picture
    Distributing Loser
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2006
    Posts
    874
    Rep Power
    203
    Could be popen 'fails' because safe_mode is on, and the resouce is outside your access range?
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    Somewhere on earth. Hmm...
    Posts
    115
    Rep Power
    19
    I have a hunch that my host(godaddy) is at fault. I am going to try the script on another server.

    [edit]
    Host is not at fault. Tried it on anohter host (http://www.fastfragclan.com/test/dir_size.php ) and same thing.
    [/edit]

    PS: PHP is not run in safe mode. http://help.godaddy.com/article.php?...83&topic_id=&&
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo