Forum Moderators: coopster
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);
$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.
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-
<?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";
}
?>