Free Web Hosting Forum
(#1 (permalink))
Old
nevermore93's Avatar
Senior Member
nevermore93 is on a distinguished road
 
Posts: 298
Join Date: Dec 2011
Location: USA
Default File Counter PHP Script - 05-14-2012, 02:24 AM

OK, we all know (or we should) that despite the very generous 1.5GB of free space that 000webhost gives us, there is a limit of 10,000 individual files for each account. If we go beyond that, we get the dreaded 552 Quota Exceeded error.

The Cpanel doesn't readily give us this info on how many files we have, so here's an easy way to check. I've put together a simple PHP script that seems to work pretty well here. Simply upload it to your "public_html" directory and then use your browser to view it.

Looks like this:


You can download the script HERE

ENJOY!!


Reply With Quote
Sponsored Links
(#2 (permalink))
Old
nevermore93's Avatar
Senior Member
nevermore93 is on a distinguished road
 
Posts: 298
Join Date: Dec 2011
Location: USA
Default 05-14-2012, 06:19 AM

Still playing around with this today, and curious to see what others are coming up with as results. I've made a change in the script already, and it seems to be getting a bit more accurate on large file counts. So far, the comparisons are pretty close. Here's what I'm getting for file counts from various sources on two of my domains.

Domain 1:
My PHP Script: 6606 Files
FTP Login Msg: 1367 Files <-- gotta be a 000webhost glitch
FTP DL Queue: 6612 Files


Domain 2:
My PHP script: 3141 Files
FTP Login Msg: 3137 Files
FTP DL Queue: 3145 Files


Reply With Quote
(#3 (permalink))
Old
d3iti's Avatar
Super Moderator
d3iti is on a distinguished road
 
Posts: 6,620
Join Date: Jul 2009
Location: Spain
Default 05-14-2012, 07:03 AM

Hello nevermore93,

A few questions about your script:

Your script is counting files or the number of files + folders?

Is your script counting the files inside the public_html folder and subfolders, or also includes the root folder?

Thanks for you script


Recuerda realizar copias de seguridad de tus sitios web. Si este mensaje te ayudó puedes pulsar sobre el botón karma
Reply With Quote
(#4 (permalink))
Old
Leder678's Avatar
Senior Member
Leder678 is on a distinguished road
 
Posts: 1,618
Join Date: Jan 2009
Location: Norway
Send a message via MSN to Leder678
Default 05-14-2012, 08:06 AM

Nice, but you should follow the Drupal standards for php files, e.g. no lines after the last ?> (the last ?> may be removed in pure php files)
and the entire count.php file could be within one set of php tags (<?php ?>)

And the code could be simplefied alot

PHP Code:
<?php
    $countdir 
0//The folder counter
    
$countfile 0//The file counter
    
$arrayf = array(); //Optinal array for filenames
    
$arrayd = array(); //Optinal array for directory names(foldernames)
    
    
function loop($path "") { //Path may be set, or not, meh, doesn't matter
        
global $countdir$countfile$arrayf$arrayd//To make this simple script possible
        
foreach(glob("$path*") as $g) { //Loops through every file in the path, and adds it to the variable "$g" (for global) to do the rest of the checks.
            
if(is_dir($g)) { //Checks wether $g is a dir or not
                
$countdir++; //Adds to folders counted
                
$arrayd[] = $g//Adds the folder name
                
loop("$g\\"); //Continues the loop, as there is probably more files inside other dirs.
            
} else { //If it's not a dir, then it's a file.
                
$countfile++; //Adds to files counted
                
$arrayf[] = $g//Adds the file name
            
}
        }
    }
    
    
loop(); //Starts the function
    
echo "Files: $countfile\r\nDirs: $countdir\r\n"//Prints out files and folders (this is from a commandline view, change "\r\n" to "<br />" if you wish to view it in the browser.
    
    /* Optinal (Delete all inbetween here if you want to :) ) */
    //Dirs
    
print_r($arrayd); //Prints all the folders we found
    
echo "\r\n"//Makes a new line (commandline view)
    //Files
    
print_r($arrayf); //Prints all the files we found
    /* END Optinal */
    
?>


Follow me on twitter @Mortenrb

W3Fools - Read and learn

Please AT LEAST read the 10 bolded lines of the TOS at:
http://www.000webhost.com/includes/tos.php

Last edited by Leder678; 05-14-2012 at 08:24 AM.
Reply With Quote
(#5 (permalink))
Old
nevermore93's Avatar
Senior Member
nevermore93 is on a distinguished road
 
Posts: 298
Join Date: Dec 2011
Location: USA
Default 05-14-2012, 12:45 PM

Quote:
Originally Posted by d3iti View Post
Hello nevermore93,

A few questions about your script:

Your script is counting files or the number of files + folders?

Is your script counting the files inside the public_html folder and subfolders, or also includes the root folder?

Thanks for you script
The script counts the number of files and all sub-folders recursively and gives a count for each.

It counts only sub-folders and files in it's parent directory and does not read the root folder. For example, if placed in "public_html" it will give output for all files and folders in that directory. If you put it in "public_html/myfolder/" it will only give results for "myfolder".

Very portable

Cheers!


Reply With Quote
(#6 (permalink))
Old
nevermore93's Avatar
Senior Member
nevermore93 is on a distinguished road
 
Posts: 298
Join Date: Dec 2011
Location: USA
Default 05-14-2012, 01:04 PM

Quote:
Originally Posted by Leder678 View Post
Nice, but you should follow the Drupal standards for php files, e.g. no lines after the last ?> (the last ?> may be removed in pure php files)
and the entire count.php file could be within one set of php tags (<?php ?>)
ya ya ya... I know it's messy. I'll get that cleaned up a bit today. I'm just happy it seems to be working at the moment.

Quote:
Originally Posted by Leder678 View Post
And the code could be simplefied alot

PHP Code:
<?php
    $countdir 
0//The folder counter
    
$countfile 0//The file counter
    
$arrayf = array(); //Optinal array for filenames
    
$arrayd = array(); //Optinal array for directory names(foldernames)
    
    
function loop($path "") { //Path may be set, or not, meh, doesn't matter 
        
global $countdir$countfile$arrayf$arrayd//To make this simple script possible
        
foreach(glob("$path*") as $g) { //Loops through every file in the path, and adds it to the variable "$g" (for global) to do the rest of the checks.
            
if(is_dir($g)) { //Checks wether $g is a dir or not
                
$countdir++; //Adds to folders counted
                
$arrayd[] = $g//Adds the folder name
                
loop("$g\\"); //Continues the loop, as there is probably more files inside other dirs.
            
} else { //If it's not a dir, then it's a file.
                
$countfile++; //Adds to files counted
                
$arrayf[] = $g//Adds the file name
            
}
        }
    }
    
    
loop(); //Starts the function
    
echo "Files: $countfile\r\nDirs: $countdir\r\n"//Prints out files and folders (this is from a commandline view, change "\r\n" to "<br />" if you wish to view it in the browser.
    
    /* Optinal (Delete all inbetween here if you want to :) ) */
    //Dirs
    
print_r($arrayd); //Prints all the folders we found
    
echo "\r\n"//Makes a new line (commandline view)
    //Files
    
print_r($arrayf); //Prints all the files we found
    /* END Optinal */
    
?>
I tried some methods like this, but didn't have the best of luck getting it to count sub folders recursively.

Here's the basic function I'm using. The rest is all just making the output "pretty" ..lol
PHP Code:
<?php
function scan_dir($dirname)   {
    
$count['Files'] = 0;
    
$count['Folders'] = 0;
    
$dir opendir($dirname);
    while ((
$file readdir($dir)) !== false) {
        if(
$file != "." && $file != "..")  {
            if(
is_file($dirname."/".$file))
                
$count['Files']++;
            if(
is_dir($dirname."/".$file)) {
                
$count['Folders']++;
                
$counts scan_dir($dirname."/".$file);
                
$count['Folders'] += $counts['Folders'];
                
$count['Files'] += $counts['Files'];
            }
        }
    }
    
closedir($dir);

    return 
$count;
}

$dirname "./";
$count scan_dir($dirname);

print_r $count );
?>
Thanks for your input. I'm trying to get more into PHP and enjoy the learning experience.


Reply With Quote
(#7 (permalink))
Old
d3iti's Avatar
Super Moderator
d3iti is on a distinguished road
 
Posts: 6,620
Join Date: Jul 2009
Location: Spain
Default 05-14-2012, 01:26 PM

Quote:
Originally Posted by nevermore93 View Post
The script counts the number of files and all sub-folders recursively and gives a count for each.

It counts only sub-folders and files in it's parent directory and does not read the root folder. For example, if placed in "public_html" it will give output for all files and folders in that directory. If you put it in "public_html/myfolder/" it will only give results for "myfolder".

Very portable

Cheers!
Thanks nevermore93 . Clear to me how your PHP script works.


Recuerda realizar copias de seguridad de tus sitios web. Si este mensaje te ayudó puedes pulsar sobre el botón karma
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com