Forum Moderators: coopster

Message Too Old, No Replies

A code that counts how many files in a folder?

         

Snickers

6:27 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



I've seen this done in images hosted on the same server...

I have an image host, and I did like a code so that I can have a counter somewhere of how many images hosted...
(So it counts the folder all uploaded pictures are in)

I've googled it ofcause, but I either am using the wrong keywords or it isn't ready on a easy-to-find spot----

Can anyone help?

carguy84

1:04 pm on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what kind of web hosting are you using, Windows based or Linux based?

Chip-

Snickers

9:02 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



I have no idea really, but I think its linux?

Anyway, I have the newest version of php (php5) and cPanel the newest version...

jatar_k

5:19 am on Jul 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe try this

<? 
$dir = '/path/to/directory/';
$filecount = 0;
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(!is_dir($f)) $filecount++;
}
}
echo 'there are ',$filecount,' files in this folder';
?>

this is completely untested but it should be close, just assign the actual path of the directory you want to count the files in to the $dir var in the beginning instead of /path/to/directory/

Snickers

3:00 pm on Jul 30, 2005 (gmt 0)

10+ Year Member



Thanks for trying, but it didn't work..

I made an empty file, posted what you wrote:

<?
$dir = '\';
$filecount = 0;
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(!is_dir($f)) $filecount++;
}
}
echo 'there are ',$filecount,' files in this folder';
?>

and it says: Parse error: parse error, unexpected T_STRING in /home2/clickspa/public_html/jpnupload/testing.php on line 11

I actually got earlier(before you posted) someone to try help me and he wrote:

<?php

$path = "\";

if ($handle = opendir($path))
{
while (false!== ($file = readdir($handle)))
{
if (!is_dir($path. $file))
{
$filecount++;
}
}
closedir($handle);

echo ('Total Images Hosted');
echo ('.$filecount.');
}

?>

and it said: Parse error: parse error, unexpected $ in /home2/clickspa/public_html/jpnupload/testing.php on line 32

Don't know if this can help? (And no, there does not exsist a line 32...)

All I know is that my server does accept php, its php5 and its a linux server...

Any help please? anyone?

BTW: that is what I want :)
(A code that tells how many files there is in a certain folder, but not counting subfolders.)

jatar_k

3:15 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that code actually works, I just tested it.

The problem with both of those pieces of code is your slash, it's going the wrong way.

$dir = '\';
or
$path = "\";

that means you are escaping the closing quote and causing the parse error. turn it around.

$dir = '/';
or
$path = "/";

that would be for the server root, I doubt that is the folder you want. If you are looking for the root of your site try this

$dir = '/home2/clickspa/public_html/';

ergophobe

4:19 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



BTW, Jatar_K's version will work for Win or Lin. You do not need to use Windows style paths (C:\something\something) in PHP and Apache. In fact, normally you shouldn't.

Snickers

4:23 pm on Jul 30, 2005 (gmt 0)

10+ Year Member



okay... I feel stupid now..

I wrote it once again:

[php]
<?php $path = "/home2/clickspa/public_html/jpnupload/"; if ($handle = opendir($path)) { while (false!== ($file = readdir($handle))) { if (!is_dir($path. $file)) { $filecount++; } } closedir($handle); $filecount = $filecount - 4; echo "Total Images Hosted: ". $filecount; }?>
[/php]

How can I make sure it does not count the subfolders in that folder, plus the subfolders files?
or does it already do that?

Snickers

9:10 am on Jul 31, 2005 (gmt 0)

10+ Year Member



ahh, I figured it out my self...

THANKS FOR THE HELP!

I appreciate it!
(You can lock this topic if you want now)