|
|
|
|
Thread title: outputing number of files in a directory |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
12-10-2006, 04:23 AM
|
#1
|
Status: Developer
Join date: Nov 2004
Location: In my house
Expertise:
Software:
Posts: 2,323
|
outputing number of files in a directory
Hey, I want to put a little thing on imghoster to say how many images are hosted.
e.g. "currently hosting xx images"
In the folder where the images are hosted, there are 2 files which must not be counted.
Thanks,
Gethn.
|
|
12-10-2006, 04:31 AM
|
#2
|
Status: Pastafarian
Join date: May 2006
Location: Duct Taped to your Ceiling
Expertise:
Software:
Posts: 3,440
|
Code:
<?php
//get path of directory containing this script
$dir = $_SERVER['DOCUMENT_ROOT'].dirname($PHP_SELF);
//open a handle to the directory
$handle = opendir($dir);
//intitialize our counter
$count = 0;
//loop through the directory
while (false !== ($file = readdir($handle))) {
//evaluate each entry, removing the . & .. entries
if (is_file($file) && $file !== '.' && $file !== '..') {
$count++;
}
}
echo $count;
?>
|
|
12-10-2006, 05:36 PM
|
#3
|
Status: Developer
Join date: Nov 2004
Location: In my house
Expertise:
Software:
Posts: 2,323
|
Thanks Blue Ire, I just tried that and it outputted 12... which is incorrect.
Im guessing it's counting the number of files in the public_html directory.
What code to I change to change the directory it counts the files in, and then how do I minus 2 for the total number of images hosted?
Cheers!
|
|
12-10-2006, 05:53 PM
|
#4
|
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
Posts: 5,200
|
Change:
$dir = $_SERVER['DOCUMENT_ROOT'].dirname($PHP_SELF);
to:
$dir = "/your/path/here/";
And you should be golden.
|
|
12-10-2006, 05:53 PM
|
#5
|
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
Posts: 3,820
|
I don't know if what you're trying to achieve would work with something this simple, but if you're looking to grab the images files from a directory then you could possibly look to using:
PHP Code:
<?php
// Find all jpg, gif or png files in the images sub-directory
$files = glob('./images/*.{jpg,gif,png}', GLOB_BRACE);
echo 'There are ' . count($files) . ' images.';
?>
|
|
12-10-2006, 07:04 PM
|
#6
|
Status: Developer
Join date: Nov 2004
Location: In my house
Expertise:
Software:
Posts: 2,323
|
Andrew R I tried that, had an error thought.
Salathe I have used your code, and it works... however it is not counting all the images, it's deducting 11 from the real number.
|
|
12-10-2006, 07:20 PM
|
#7
|
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
Posts: 3,820
|
My script only catches files ending in .jpg .gif or .png. If there are other file extensions that you want to count then you'll need to add those in as well.
|
|
12-10-2006, 07:29 PM
|
#8
|
Status: Developer
Join date: Nov 2004
Location: In my house
Expertise:
Software:
Posts: 2,323
|
I added bmp before I posted, I just tried adding jpeg to it and it added 1 more onto the final count. Is there anyway of like adding on 10?
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|