Jump to content

Counting files in a folder


Kitty

Recommended Posts

Hello everyone who's reading this! :) I have a script that counts how many files I have in a specific folder, but I recently added index.html files with a redirect script in them to all of my folders and now I don't want my script to count that index.html file. So, is it possible to make it count all files except for 1? This is the script I'm using:

 

<?php

$path_layouts = opendir("/home/***/public_html/graphics/layouts");

while ($directory_layouts = readdir($path_layouts))
{
if ($directory_layouts != "." && $directory_layouts != "..")
{
$counter_layouts++;
}
}

echo $counter_layouts;

closedir($path_layouts);

?>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/
Share on other sites

<?php

$path_layouts = opendir("/home/***/public_html/graphics/layouts");

while ($directory_layouts = readdir($path_layouts))
{
if ($directory_layouts != "." && $directory_layouts != ".." &&  $directory_layouts != "index.html")
{
$counter_layouts++;
}
}

echo $counter_layouts;
closedir($path_layouts);

?>

Why not just do this:

 

<?php

 

$path_layouts = opendir("/home/***/public_html/graphics/layouts");

 

while ($directory_layouts = readdir($path_layouts))

{

if ($directory_layouts != "." && $directory_layouts != ".." || $directory_layouts != "index.html")

{

$counter_layouts++;

}

}

 

echo $counter_layouts;

 

closedir($path_layouts);

 

?>

 

If you have several files that you dont want counted put them in an array...

No problem.  One thought, I think you meant

&& $directory_layouts != "index.html"

 

as

 

|| $directory_layouts != "index.html")

 

will cause the conditional to return true if $directory_layouts != "index.php" even if $directory_layouts == ".." or ".".

 

I think it was just a mistype, but I thought I'd point it out so no one gets confused.

 

Best,

 

Patrick

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.