Forum Moderators: coopster

Message Too Old, No Replies

count files in directory, exclude sub directories

stumped

         

dnb_ovst

7:28 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



Hello fine fellows.

Below is my script i have to find all htm/jpegs within a requested directory and embed them into the page. Right now, i am bashing my face against the wall trying to get a simple file counter to work...

What i have right now *works*, however it counts the subdirectories as well... I would like to exclude any subdirectories from the file tally.

Ive gotten EXCELLENT responses to my problems on this forum in the past and i am trying my luck again with this new problem..

If anyone has any insight or a solution, it would be greatly appreciated!

$dir = 'classifieds/'."$_GET[subcategory]";
$fp = @opendir($dir);
$filecount = @scandir($dir);

if ($file =!@readdir($fp)) {
echo "<p>No ads for this category this week. Better luck next week!</p>\n";
echo "<p id='tally'><strong>0</strong> ads found</p>";
}

else {
while ($file = @readdir($fp)) {
if (preg_match("/^[a-zA-Z0-9\/\-\_]+\.htm$/", $file)) {
require_once($dir.'/'.$file);
echo "\t\t\t\t<br /><hr /><br />\n";
}
elseif (preg_match("/^[a-zA-Z0-9\/\-\_]+\.jpg$/", $file)) {
echo "\t\t\t\t<img src='$dir/$file' alt='advertisement' />\n";
echo "\t\t\t\t<br /><br /><hr /><br />\n";
}

/* HERE LIES THE PROBLEM... */
if (!is_dir($filecount)) {$num_of_files = count($filecount);}
else {die('there is an error');}
if (isset($num_of_files) &&!empty($num_of_files)) {
if ($num_of_files == 1) {echo "<p id='tally'><strong>1</strong> ad found</p>";}
else {echo "<p id='tally'><strong>$num_of_files</strong> ads found</p>";}
}
/* END PROBLEM */
}
}
@closedir($fp);

Psychopsia

7:44 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



Hello!

Do you want to count only images and files? or what filetypes?

dnb_ovst

7:56 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



preferrably all file types (for the sake of scalability); however for right now, counting just html & jpegs will work-

hopefully as i gain more php experience later on i will be able to tailor it for the former ..

Psychopsia

8:10 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



Ok, try this:

$dir = 'classifieds/' . $_GET[subcategory];

/*check directory*/
if (!file_exists($dir))
{
die('directory not found');
}

$fp = @opendir($dir);

$num_of_files = 0;
while ($file = @readdir($fp))
{
if (preg_match("/^[a-zA-Z0-9\/\-\_]+\.htm$/", $file))
{
require_once($dir.'/'.$file);
echo "\t\t\t\t<br /><hr /><br />\n";
$num_of_files++;
}
elseif (preg_match("/^[a-zA-Z0-9\/\-\_]+\.php$/", $file))
{
echo "\t\t\t\t<a>'$dir/$file'</a>\n";
echo "\t\t\t\t<br /><br /><hr /><br />\n";
$num_of_files++;
}
}
@closedir($fp);

if ($num_of_files)
{
$str = ($num_of_files == 1)? '<strong>1</strong> ad found' : '<strong>$num_of_files</strong> ads found';
echo "<p id='tally'>$str</p>";
}
else
{
echo "<p>No ads for this category this week. Better luck next week!</p>\n";
echo "<p id='tally'><strong>0</strong> ads found</p>";
}

Also you should check the existence of $dir with file_exists [php.net] for security.

dnb_ovst

8:37 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



Hi Psychopsia,

Thanks for the suggestion! That solved my problem with the counter skipping subdirectories, however I should have stated what was really the issue at hand:

here it is,

upon landing on the web page, the client is presented with a <select> box containing categories of classified ads. The problem was that, the counter (<p id="tally">) is shown.

Before it said '54 ads found' (now it says '0 ads found' (thx for the solution btw). I was actually trying to get it to show only when the client queries the form. For now, i commented out this line of code:

else
{
echo "<p>No ads for this category this week. Better luck next week!</p>\n";
echo "<p id='tally'><strong>0</strong> ads found</p>";
}

This solves the problem in a half-assed kinda way.. The ad counter no longer shows upon landing on the page... but now the categories with zero ads do not give a "0 ads found confirmation" message...

Im curious.. is there a way to run that specific 'counter function' only upon form submission? Or is there a better way around this? Thanks again for your help-

Psychopsia

11:57 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



So, do you want to build a <select> tag with the (html and jpg) files in selected directory?
Do you need to show categories with 0 items in the <select>?

Yes it is possible to run that code after form submit, I do this with:

if (isset($_POST['submit'])) ...

dnb_ovst

6:50 pm on Sep 28, 2006 (gmt 0)

10+ Year Member



thanks psychopsia- after a few cigarettes i got it figured out, heres the final code-

<?php
$dir = 'classifieds/' . $_GET[subcategory];
$fp = @opendir($dir);
$num_of_files = 0;

if ($file =!@readdir($fp)) {
echo "<p>No ads for this category this week. Better luck next week!</p>\n";
echo "<p id='tally'><strong>0</strong> ads found</p>";
}
else {
while ($file = @readdir($fp))
{
if (preg_match("/^[a-zA-Z0-9\/\-\_]+\.htm$/", $file)) {
require_once($dir.'/'.$file);
echo "\t\t\t\t<hr />\n";
$num_of_files++;
}
elseif (preg_match("/^[a-zA-Z0-9\/\-\_]+\.jpg$/", $file)) {
echo "\t\t\t\t<img src='$dir/$file' alt='advertisement' />\n";
echo "\t\t\t\t<hr />\n";
$num_of_files++;
}
}
}
@closedir($fp);

if ($num_of_files) {
$str = ($num_of_files == 1)?'<strong>1</strong> ad found':"<strong>$num_of_files</strong> ads found";
echo "\t\t<p id='tally'>$str</p>\n";
}
?>