FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

How many files are in a directory?

 


Diablosblizz
Hi, I was wondering if it was possible to "get" how many files there are in a folder?

For example:

There will be 59 files in one directory. Then the php code would say:

There are 59 tutorials. View


Where the View is I want to put in a link, that goes to that folder.

Thanks!
hexkid
Diablosblizz wrote:
There will be 59 files in one directory. Then the php code would say:

There are 59 tutorials. View


Where the View is I want to put in a link, that goes to that folder.


Code:
<?php
$directory = 'tutorials/';
$foobar = "ls -F $directory | grep -v '/\$' | wc -l"; ### :)
$n = trim(shell_exec($foobar));
echo 'There are ', $n, ' tutorials. <a href="', $directory, '">View</a>';
?>
Diablosblizz
All I get from that is:

Quote:
There are tutorials. View



I named the folder as specified, and it gives me that? Any other ideas?

Thanks.
hexkid
Diablosblizz wrote:
All I get from that is:

Quote:
There are tutorials. View



I named the folder as specified, and it gives me that?

shell_ exec() needs safe mode OFF. I tested the script on FriHost before I posted it.

Diablosblizz wrote:
Any other ideas?

readdir() and friends.
Diablosblizz
Okay, it works. I was testing it on my home server before I uploaded it onto Frihost. I guess Xampp doesn't have Save Mode Off?
hexkid
Diablosblizz wrote:
I guess Xampp doesn't have Save Mode Off?

You have to configure it. There are three "php.ini". Which is now the correct one?
Diablosblizz
Sorry this is kinda of a bumb. But..

My safemod is set to "Off".

Code:
;
; Safe Mode
;
safe_mode = Off



What the heck?
hexkid
Diablosblizz wrote:
But..

My safemod is set to "Off".


What the heck?


Hmmm, you're on Windows, right?

ls, grep, and wc are all Un*x programs Smile
kv
Here you go
Code:

function CountFiles($dir)
{
   $cnt=0;
   if (is_dir($dir))
   {
      if ($dh = opendir($dir))
      {
         while($file = readdir($dh))
            $cnt++; //if you want to count just files and not dirs, put a if(is_dir($file)) before $cnt++;
           closedir($dh);
         
      }
   }
   return $cnt;
}
Diablosblizz
Quote:
Hmmm, you're on Windows, right?

ls, grep, and wc are all Un*x programs Smile


Yes I am on windows.

Quote:
function CountFiles($dir)
{
$cnt=0;
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while($file = readdir($dh))
$cnt++; //if you want to count just files and not dirs, put a if(is_dir($file)) before $cnt++;
closedir($dh);

}
}
return $cnt;
}


What does this do?
kv
Quote:


What does this do?


Returns the number of files in a given directory. It is php and hence works even if shell_exec() is disabled.

Just pass the directory path to the function and it returns the number of files.
sonam
Code:
<?php
$dir = "tutorials";
$openDir  = opendir($dir);
while (false !== ($fileNames = readdir($openDir))) {
   if(is_dir("$dir/$fileNames") !== FALSE) {
      continue;
   } else {
      $files[] = $fileNames;
      sort($files);
   }       
}
$count = count($files);
echo "There are $count tutorials. <a href=$dir>View!</a>";
?>


Sonam
manum
this script will list all files in a directory and can even list directories if u want to..........


<?php

// This is the directory to list files for.
$theDirectory = ".";
// Do you want to show directories? change to false to hide directories.
$listDirectories = true;

if(is_dir($theDirectory))
{
echo "<table><tr><td>Name</td><td>Type</td><td>Size</td></tr>";
$dir = opendir($theDirectory);
while(false !== ($file = readdir($dir)))
{
$type = filetype($theDirectory ."/". $file);
if($listDirectories || $type != "dir")
{
echo "<tr><td>" . $file . "</td>";
echo "<td>" . $type . "</td>";
echo "<td>";
if($type == "file")
echo filesize($file);
echo "</td></tr>";
}
}
closedir($dir);
echo "</table>";
}
else
{
echo $theDirectory . " is not a directory";
}
?>
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.