SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Nov 20, 2004, 15:13 #1
- Join Date
- Dec 2003
- Location
- el paso tx
- Posts
- 119
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
list and/or count filetypes (*.jpeg)
hey all.
i am using the following function to count the number of files in a specified directory:
Code://=> Opens specified DIR and counts the files or folders within it. function getFileCount ($type,$dir='./'){ if (($type == 'file') || ($type == "link") || ($type == 'dir')) { if (is_dir($dir)) { $fd = opendir($dir); while ($part = readdir($fd)) { if ($part != "." && $part != "..") { clearstatcache(); $function = 'is_' . $type; if ($function($dir . $part)) { $count++; } } } }else{ $err = $dir . ' is not a directory'; } }else{ $err = $type . ' is unknown type -- must be file,link or dir'; } if (isset($err)) { Return $err; }else{ Return $count; } /*- Example Use print getFileCount('file','thumbs/'); print getFileCount('file','full_size/'); -*/ }
im using this for a gallery im writing.
i have something like this on my index.php page:
Code:if ((getFileCount('file',"$file/")) < (getFileCount('file',"$file/thumbs/"))) { echo "too many thumbs"; }elseif((getFileCount('file',"$file/")) > (getFileCount('file',"$file/thumbs/"))) { echo "<a href=\"thumb.php?file=$file\">Make Thumb</a>"; }elseif((getFileCount('file',"$file/")) == (getFileCount('file',"$file/thumbs/"))) { echo "DONE!"; }
if FULL_SIZE == THUMBS, then nothing needs to be done.
but if FULL_SIZE > THUMBS, then thumbnails need to be created.
<< with the code posted here in my other thread: http://www.sitepoint.com/forums/showthread.php?t=211101 >>
then i came to realize that the thumb.php file needs to reside in the FULL_SIZE folder and i got it to create the THUMBS folder, if it doesnt already exist.
my problem is that i am trying to get my getFileCount function to ONLY count the jpegs.
any help would be dearly appreciated.
-
Nov 20, 2004, 15:17 #2
- Join Date
- Nov 2003
- Location
- Sri Lanka
- Posts
- 156
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well what about haing an if condition to count the second index of the getimagesize() function? Look up the predefined IMAGETYPE constant.
-
Nov 20, 2004, 19:05 #3
- Join Date
- Nov 2004
- Location
- Boston
- Posts
- 25
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi
simple enough...
Here is a simple example for you!
As a function!
PHP Code:
<?
function this_ext ( $file )
{
return substr ( $file, strrpos ( $file, "." ) + 1 );
}
function get_total ( $dir, $match )
{
$out = 0;
if ( $dh = opendir ( $dir ) )
{
while ( false !== ( $file = readdir ( $dh ) ) )
{
if ( $file != "." && $file != ".." )
{
if ( is_file ( $dir . $file ) && in_array ( this_ext ( $file ), $match ) )
{
$out++;
}
}
}
closedir ( $dh );
}
return ( $out );
}
// unix, the directory you are counting the content from! (include the trailing '/')
//$path = "./path/www/docs/";
// windows, the directory you are counting the content from! (include the trailing '/')
$path = "e:/www/docs/img/";
// an array of files to count!
$count = array ( 'jpg', 'jpeg' );
// call the function....
$total = get_total ( $path, $count );
// show the results...
echo $total;
?>
J!
-
Nov 20, 2004, 21:18 #4
- Join Date
- Dec 2003
- Location
- el paso tx
- Posts
- 119
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by jbr
ill give it a try tomorrow.
let you know how it goes.
-
Nov 21, 2004, 03:22 #5
- Join Date
- Nov 2003
- Location
- Sri Lanka
- Posts
- 156
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PS: I made a mistake earlier, I meant third index of the getimagesize() function, not second
jbr's code would work, but it won't always be 100% accurate as far as I can see because a gif can have a .jpg extension. It's the header of that image that actually determines what type it is...so if you want to be 100% sure your images are jpg's you should use getimagesize() IMO
I just modified your code a bit jbr, I hope you don't mind
PHP Code:<?php
function get_total($dir, $allowedTypes = array())
{
$out = 0;
if($dh = opendir($dir))
{
while(false !== ($file = readdir($dh)))
{
if($file != "." && $file != "..")
{
list($width, $height, $type, $attr) = getimagesize($file);
if(is_file($dir . $file))
{
foreach($allowedTypes as $allowed)
{
switch($allowed)
{
case $type:
$out++;
break;
default:
break;
}
}
}
}
}
closedir($dh);
}
return $out;
}
// unix, the directory you are counting the content from! (include the trailing '/')
//$path = "./path/www/docs/";
// windows, the directory you are counting the content from! (include the trailing '/')
$path = "e:/www/docs/img/";
// an array of files to count!
// call the function....
//See: http://www.php.net/getimagesize/ for more
$count = array( 2 /*JPG*/,
10 /*JP2*/);
$total = @get_total( $path, $count );
// show the results...
echo $total;
?>
PS: Calling the function prepended with an '@' will set PHP in silent mode for that call and any warning messages will not be displayed.
Bookmarks