How to show count of files in a dir?

Started on the old forum in August 2012 · view the original in the archive · old links redirect here

4 replies · 718 views · started 14 years ago · 1 min read

4 participants
Smwap Newbie · 3 posts #1

here is my site index code

<?php
include('header.php');
include('config.php');
include('display_no.php');
include 'extra.php';

if(in_array($_REQUEST['dir'],$ex))
{
@$_REQUEST['dir']();
include 'footer.php';
exit;
}

if(substr($_REQUEST['dir'],-2)=='..')

{ die("Wrong Path !");
}
function listFiles( $from = '.')
{
if(! is_dir($from))
return false;

$files = array();
$dirs = array( $from);
echo '';
while( NULL !== ($dir = array_pop($dirs)))
{
if( $dh = opendir($dir))
{
while( false !== ($file = readdir($dh)))
{
if( $file == '.' || $file == '..' || $file == 'description.txt' || substr($file,-11,7) == '-tthumb' || substr($file,-9)=='error_log' || substr($file,-9)=='.htaccess' || substr($file,-4)=='.php')
continue;
$path = $dir . '/' . $file;
if( is_dir($path)){
//$size = dirSize($path);
$files['dir'][uniqid(filemtime($path))] =  array($file,$path,substr($path,strrpos($path, ".")+1));
}
else{
if($_REQUEST['sort']=='1')	// sort by new to old
$files['files'][$file] = array($file,$path,format_size(filesize($path)));
else		// sort by a-z
$files['files'][uniqid(filemtime($path))] = array($file,$path,format_size(filesize($path)));
}
}
closedir($dh);
}
}
echo '';
return $files;
}

/*
::Prem=>: function profile()
this function calles fileList function to get list of files and directories
:Prem:
*/
function profile( $func, $trydir)
{
$list = $func( $trydir);
//echo 'Finished : '.count($list['files']).' files</pre>';
return $list;
}

/*
=> Do not change bellow: function format_size()
this function convert size from
BYTES to KB, MB, GB, TB
and return

*/
function format_size($size, $round = 2) {
$sizes = array('Byts', 'kb', 'mb', 'gb','TB');
$total = count($sizes)-1;
for ($i=0; $size > 1024 && $i < $total; $i++) $size /= 1024;
return round($size,$round).$sizes[$i];
}
if(isset($_POST['dir']))
{
$dir = "/";
$file = $_POST['file'];
$name = $_POST['sdir'];
if($content = file_get_contents($file))
{
$fp = fopen($dir.'/'.$name, 'w');
fwrite($fp, $content);
fclose($fp);
}}
if($_REQUEST['dir'])
$listof = $_REQUEST['dir'];
else
$listof = directoryPath;

// calling function to get file lists
if(is_dir($listof))
{

$list = listFiles($listof);
//echo '<h2>'.str_replace('/','  ',$listof).'</h2>';
$category = explode('/',$listof);
echo (empty($_REQUEST['dir']) || $_REQUEST['dir']=='menu'?'<div class="menubar">Main Menu</div><div class="item2"><table><tr><td>»</td><td><a href="/mp3">Music</a></td></tr></table></div>':'<div class="menubar">'.ucwords($category[count($category)-1])).' </div>';
if(!empty($_GET['dir'])){ echo '<div align="Center">Page (';
if(count($list['dir']))
echo (empty($_REQUEST['sdir'])?'1':$_REQUEST['sdir']).'/'.ceil(count($list['dir'])/dirPerPage);
else
echo $_REQUEST['start'].'/'.ceil(count($list['files'])/filesPerPage);
echo ')</div>'; }

if(is_file($listof.'/description.txt')){
$sfile = $listof.'/description.txt';
$desctiption = implode('<br/>', file($sfile));
echo '<div class="desc">'.$desctiption.'</div>';
}
if(count($list['dir'])){
(empty($_REQUEST['dir']) || $_REQUEST['dir']=='menu'?krsort($list['dir']):asort($list['dir']));

/*
::>townwap@gmail.com: printing dir list to screen
*/
$sdir = ($_REQUEST['sdir'] ? $_REQUEST['sdir'] : 1);
$display = dirPerPage * ($sdir-1);
$cat=1;
foreach($list['dir'] as $key){
if($cat > $display && $cat <= $display+dirPerPage){
$class=($cat%2==0?'item2':'item1');
echo "<div class='$class'><table><tr><td>» </td><td><a href='/".$key[1].".html'>".ucwords(str_replace('-',' ',$key[0]))."</a></td></tr></table></div>";
}
$cat++;
}
echo '';
;	echo '';
//$mLink = $_SERVER['PHP_SELF'] . "?";
$mLink ='-dir/'.$_GET['dir'].'.html';

include_once('display_no.php');
display_pageno(count($list['dir']),$sdir,$mLink,dirPerPage);

}

if(count($list['files'])){
if($_REQUEST['sort']=='1'){	// Sort By New To Old
sort($list['files']);
echo '<div align="Center"><a href="/sort/2/'.$_REQUEST['dir'].'.html">Sort by New to Old</a></div>';
}
else{	// sort by a-z
krsort($list['files']);
echo '<div align="Center"><a href="/sort/1/'.$_REQUEST['dir'].'.html">Sort by A-Z</a></div>';
}
/*
::>townwap@gmail.com: printing file list to screen
*/
$start = $_REQUEST['start']?$_REQUEST['start']:1;
$display = filesPerPage * ($start-1);
$c=1;
foreach($list['files'] as $key){
if($c>$display && $c<=$display+filesPerPage){

$link='<a href="/file/'.$key[1].'.html">'.str_replace('-',' ',str_replace('(IndiaMob.in)','',$key[0])).'</a> ('.$key[2].')';
// echo "<div style='padding:1px;padding-left:6px;'>";
echo "<div class='dash'>";
// $thumb = str_replace($key[0],'thumb-'.$key[0],$key[1]);
$thumb = substr($key[1],0,strrpos($key[1],'.')).'-tthumb.gif';
$thumb1 = substr($key[1],0,strrpos($key[1],'.')).'-tthumb.jpg';
$thumb2 = substr($key[1],0,strrpos($key[1],'.')).'-tthumb.png';
if(is_file($thumb)){
echo "<table><tr><td class='page'><img src='/$thumb' width='45' height='60' /></td><td>$link</td></tr></table></div>";
}
elseif(is_file($thumb1)){
echo "<table><tr><td class='page'><img src='/$thumb1' width='45' height='60' /></td><td>$link</td></tr></table></div>";
}

elseif(is_file($thumb2))
{
echo "<table><tr><td class='page'><img src='/$thumb2' width='45' height='60' /></td><td>$link</td></tr></table></div>";
}
elseif(substr($key[1],strrpos($key[1],'.')+1)=='jpg' || substr($key[1],strrpos($key[1],'.')+1)=='jpeg' || substr($key[1],strrpos($key[1],'.')+1)=='JPG'
|| substr($key[1],strrpos($key[1],'.')+1)=='gif'
|| substr($key[1],strrpos($key[1],'.')+1)=='GIF'
|| substr($key[1],strrpos($key[1],'.')+1)=='PNG'
|| substr($key[1],strrpos($key[1],'.')+1)=='png'){
echo '<table><tr><td class="page"><img src="/'.$key[1].'" width="45" height="60" /></td><td>'.$link.'</td></tr></table></div>';
}
elseif(substr($key[1],strrpos($key[1],'.')+1)=='3gp' || substr($key[1],strrpos($key[1],'.')+1)=='mp4' || substr($key[1],strrpos($key[1],'.')+1)=='3GP'
|| substr($key[1],strrpos($key[1],'.')+1)=='MP4'){

echo '<table><tr><td class="page"><img src="/ffmpeg.php?c='.$key[1].'&h=60&w=45" /></td><td>'.$link.'</td></tr></table></div>';
}
elseif(substr($key[1],strrpos($key[1],'.')+1)=='nth' || substr($key[1],strrpos($key[1],'.')+1)=='thm')

{

echo '<table><tr><td class="page"><img src="/thm.php?H=60&W=45&thm='.$key[1].'" /></td><td>'.$link.'</td></tr><table></div>';
}
else {
echo "<table><tr><td>» </td><td>".$link."</td></tr></table></div>"; }
}
$c++;
}
echo '';
;	echo '';
$mLink=(empty($_GET['sort'])?'':'/sort/'.$_GET['sort']).'/'.$_GET['dir'].'.html';
//$mLink = substr($mLink,0,strpos($mLink, '&start='));
//$mLink = $mLink."&start=";

include_once('display_no.php');
display_pageno(count($list['files']),$start,$mLink,filesPerPage);

}
}
else
{
echo '<br/><h2>Error: wrong path entered...</h2><br/>';
}

/*
Go back to parent dir
*/
echo '';
echo '';
echo '</div>';

if(empty($_GET['dir']))
{ extra();
}
include_once('footer.php'); ?>
</body></html>

Can any one mod it to show count of files in a dir? for example Games [1034] Ringtonesm [887]

It showing only Games Ringtones

arnage Lead · 811 posts #2

Try this.

$counted = count($list['files']);
echo $counted;
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Smwap Newbie · 3 posts #3

Not working.. Only showing 0 Games [0] Themes [0]

ozziemale31 Lead · 861 posts #4

re

Smwap wrote: Not working.. Only showing 0 Games [0] Themes [0]

then it is working files is the directory u want to count so change that to the folder name u want to count specificaly ie : $list['games'] is your dirctory

$game = count($list['games']);
$ring = count($list['ringtones']);
$wall = count($list['wallpapers']);
$theme= count($list['themes']);
echo $game;  
echo $ring;
echo $wall;
echo $theme;

External image (may be gone) External image (may be gone)

External image (may be gone)

*****

**Dont Ask Me Dumb Questions.Or you'l get a Dumb Answer.. Want A Profesional Logo or Theme For Your wap site Pm Me.If I Have The Time Ill Make It For Free ***

Andrew. Newbie · 7 posts #5

Here, I wrote this function for you, it should do the job:

function countfiles($dir,$ext='')
{
	$i=0;
	$dir=trim($dir,'/');
	$ext=(!empty($ext)) ? trim($ext,'.') : '';
	if(!is_dir($dir))
	{
		trigger_error('String provided is not a valid directory path',E_USER_WARNING);
		return '0';
	}
	$files=scandir($dir);
	foreach($files as $file)
	{
		if(is_file($dir.'/'.$file))
			if(empty($ext))
				$i++;
			else
				if(substr($file,-4)=='.'.$ext)
					$i++;
	}
	return $i;
}

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close