Hi, here is my code so far:

<?php
//List files in chosen directory
	$dir = opendir("uploads");
	while ($entryName = readdir($dir))
	{
		$dirArray[] = $entryName;
	}
	closedir($dir);
	
	$indexCount = count($dirArray);
	echo "$indexCount files <br />";
	
	for($index=0; $index < $indexCount; $index++)
	{
		if ($dirArray[$index] != "." && $dirArray[$index] != "..")
		{
			$type = filetype($dirArray[$index]);
			if ($type == "dir")
			{
				echo "<img src='dir.png' />";
			}
			else
				{
					echo "<img src='file.png' />";
				}
			
			echo "<a href='".$dirArray[$index]."'>".$dirArray[$index]."</a> <br />";
			
		}
	}
?>

My problem is that I when the indexCount, counts all my files, I just want it to display how many files there are of a chosen extension.
Let's say I just want it to tell me how many files I have of .jpg or .jpg and .png

Can someone help me with this?


Help will be much appriciated.

Recommended Answers

All 3 Replies

use this code to validate extension:

echo pathinfo($dirArray[$index], PATHINFO_EXTENSION);

Filetype is't what u want.
check for gif file

if (exif_imagetype($dirArray[$index]) == IMAGETYPE_GIF)
                     $NumberOfGif++;

to check for other image types see
http://nl.php.net/manual/en/function.exif-imagetype.php
This look inside the file and is therefor slow but gifs u the true type of the image
a .png renamed .gif wil be recognised as png

to simply check the exstenion or check for non images (faster!)
use

$path_parts = pathinfo($dirArray[$index]);
if ( $path_parts['extension']=='gif')
               $NumberOfGif++;

I know this will probably be a more advanced response than what you are looking for but there are a lot of advantages to this kind of code in terms of re-usability. So I'm going to post this hoping it helps you as well as anyone else who needs to solve the same kind of problem.

<?php

/**
 * ExtensionFilterIterator Class
 * Used to filter a directory iterator by any number of extensions
 * 
 * @link http://www.php.net/manual/en/class.filteriterator.php
 */
class ExtensionFilterIterator extends FilterIterator
{
	/**
	 * Stores an array of extensions to filter on
	 * @var array
	 */
	protected $_extensions = array();
    
	
	/**
	 * Creates a class instance and sets extensions to filter on
	 * @param Iterator $iterator
	 * @param array|string $extension
	 */
	public function __construct( Iterator $iterator , $extension )
	{
		parent::__construct( $iterator );
		if( is_string( $extension ) ){
			$this->_extensions[] = $extension;
		}
		
		if( is_array( $extension ) ){
			$this->_extensions = $extension;
		}
    }
    
    /**
     * Implementation of abstract accept method
     * Determines if item is a file and also if the file has a valid extension
     * 
     *@see FilterIterator::accept()
     */
    public function accept()
    {
        $item = $this->getInnerIterator()->current();
		if( $item->isFile() && in_array( substr( $item->getFilename(), ( strrpos( $item->getFilename(), '.') + 1 ) ), $this->_extensions ) ){
			return true;
		}
        return false;
    }
}

Once you have the ExtensionFilterIterator defined in a file that you can include/require into your project its usage is simple.

//Defile the path to the image directory
$path = 'images/';

//Create a DirectoryIterator instance with the path and wrap that instance with
//an instance of the ExtensionFilterIterator
$iterator = new ExtensionFilterIterator( new DirectoryIterator( $path ), array( 'jpg', 'png', 'gif' ) );

//User iterator_count function to count the number of items that pass through the filter
//@link http://php.net/manual/en/function.iterator-count.php
echo iterator_count($iterator);

Whats nice about this is not only can you count it for your numbers, but you can also loop over it.

//Defile the path to the image directory
$path = 'images/';

//Create a DirectoryIterator instance with the path and wrap that instance with
//an instance of the ExtensionFilterIterator
$iterator = new ExtensionFilterIterator( new DirectoryIterator( $path ), array( 'jpg', 'png', 'gif' ) );

foreach( $iterator as $file ){
    echo $file->getFilename().PHP_EOL; //each file item is an instance of SplFileInfo
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.