Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1662 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

outputing number of files in a directory

Thread title: outputing number of files in a directory
Closed Thread    
    Thread tools Search this thread Display Modes  
12-10-2006, 04:23 AM
#1
Gethin is offline Gethin
Gethin's Avatar
Status: Developer
Join date: Nov 2004
Location: In my house
Expertise:
Software:
 
Posts: 2,323
iTrader: 0 / 0%
 

Gethin is on a distinguished road

Send a message via MSN to Gethin

  Old  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
Awesome is offline Awesome
Awesome's Avatar
Status: Pastafarian
Join date: May 2006
Location: Duct Taped to your Ceiling
Expertise:
Software:
 
Posts: 3,440
iTrader: 26 / 93%
 

Awesome is on a distinguished road

  Old

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
Gethin is offline Gethin
Gethin's Avatar
Status: Developer
Join date: Nov 2004
Location: In my house
Expertise:
Software:
 
Posts: 2,323
iTrader: 0 / 0%
 

Gethin is on a distinguished road

Send a message via MSN to Gethin

  Old

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
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

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
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

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
Gethin is offline Gethin
Gethin's Avatar
Status: Developer
Join date: Nov 2004
Location: In my house
Expertise:
Software:
 
Posts: 2,323
iTrader: 0 / 0%
 

Gethin is on a distinguished road

Send a message via MSN to Gethin

  Old

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
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

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
Gethin is offline Gethin
Gethin's Avatar
Status: Developer
Join date: Nov 2004
Location: In my house
Expertise:
Software:
 
Posts: 2,323
iTrader: 0 / 0%
 

Gethin is on a distinguished road

Send a message via MSN to Gethin

  Old

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?

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed