listing directory and filtering

Hi:

This is real easy for some but need help with this script. As it is now the script list all html files in a directory so it filters out all files except files ending in .html. I need the script to preform a couple more function and need help.

1) It needs to display just the name of the file without the extension (example: index.html would be diplayed as index but the link will point to index.html

2.) Filter out any .html files that have -days.html or -months.html and exclude those from the printed list (example: test-days.html or test-months.html)

Thanks in advance for anyones help.

Here is the code:

<?
echo "Click on customer to view transfer stats<br>";
$handle = opendir('.');

closedir($handle);
$handle=opendir('.');
$counter = 0;
while ($file = readdir($handle))
{
$the_type = strrchr($file, ".");
$allow_file = eregi("html",$the_type);
if ($file != "." and $file != ".." and $allow_file)
{
$myfiles[$counter] = $file;
echo "<br>  <a href=\"$file\">$file</a>\n";
$counter++;
}
}
closedir($handle);
?>
fin51Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VGRCommented:
<?
echo "Click on customer to view transfer stats<br>";
$handle = opendir('.');

closedir($handle);
$handle=opendir('.');
$counter = 0;
while ($file = readdir($handle))
{
$the_type = strrchr($file, ".");
$allow_file = eregi("html",$the_type);
if ($file != "." and $file != ".." and $allow_file)
{
     // get name without extension
     // i don't like this : $filename = current(explode('.', basename($file)));
     // I prefer good ol' style
     if ($a=strpos($file,'.')) {
       $ext=substr($file,$a); // . inside
       $filename=substr($file,0,$a);
     } else { $filename=$file; $ext=""; }
     // test for presence of -days, -month
     $ok=(! (strpos($filename,'-month')===FALSE));
     $ok=($ok AND (! (strpos($filename,'-day')===FALSE)));
     // if ok, memorize and print
     if ($ok) {
       $myfiles[$counter] = $file;
       // printout
       echo "<br>  <a href=\"$filename$ext\">$filename</a>\n";
       $counter++;
     } // if ok
}
}
closedir($handle);
?>

0
fin51Author Commented:
I tried it and filters out everything and does not display any files.

I have test.html test-days.html and test-months.html in the same directory

It should show test on the output with a link to test.html but instead it shows nothing. If you could give me a idea of what wrong that would be great.
0
VGRCommented:
sorry, I miswrote the tests

suppress the NOT (!)

I'm stupid :D

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

VGRCommented:
BTW, Note that I'm not responsible for this :
$the_type = strrchr($file, ".");
$allow_file = eregi("html",$the_type);

I would never have written this that way just to extract the file extension and compare it to '.html' ;-)
0
fin51Author Commented:
I tried it and filters out everything and does not display any files.

I have test.html test-days.html and test-months.html in the same directory

It should show test on the output with a link to test.html but instead it shows nothing. If you could give me a idea of what wrong that would be great.
0
VGRCommented:
You may also be interested by a question justet answered yesterday evening and that should be easily visible in the "last questions answered" ;-)

<?php

$directory = "./";
if($dir_handle = opendir($directory)){
 while($file = readdir($dir_handle))
 if (! is_dir($file)) // candidate ok : file
   if (($file <> '.') AND ($file <> '..')) { // file OK
     // get name without extension
     // i don't like this : $filename = current(explode('.', basename($file)));
     // I prefer good ol' style
     if ($a=strpos($file,'.')) {
       $ext=substr($file,$a); // . inside
       $filename=substr($file,0,$a);
     } else { $filename=$file; $ext=""; }
     // printout
    echo "<a href=\"$directory$filename$ext\">$filename</a><br>";
   } // if file OK
} // if dir opened OK
closedir($dir_handle);
?>
0
fin51Author Commented:
Sorry but what do you mean by supressing the NOT (!)
Just trying to learn and understand. This is new to mean.
0
fin51Author Commented:
Ok the last one works fine but it show files with  -days and -months but everything else is working perfect.
0
fin51Author Commented:
Ok the last one works fine but it show files with  -days and -months but everything else is working perfect.
0
VGRCommented:
of course, it wasn't adapted to your specific needs. Also note it skips directories

Here's a modified one for your problem :

<?php

$directory = "./";
if($dir_handle = opendir($directory)){
while($file = readdir($dir_handle))
if (! is_dir($file)) // candidate ok : file
  if (($file <> '.') AND ($file <> '..')) { // file OK
    // get name without extension
    // i don't like this : $filename = current(explode('.', basename($file)));
    // I prefer good ol' style
    if ($a=strpos($file,'.')) {
      $ext=substr($file,$a); // . inside
      $filename=substr($file,0,$a);
    } else { $filename=$file; $ext=""; }
    // test for presence of -days, -month
    $ok=(strpos($filename,'-month')===FALSE);
    $ok=($ok AND (strpos($filename,'-day')===FALSE));
    // if ok, memorize and print
    if ($ok) {
      // printout
      echo "<a href=\"$directory$filename$ext\">$filename</a><br>";
    } // if file not days nor month(e)s
  } // if file OK
} // if dir opened OK
closedir($dir_handle);
?>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VGRCommented:
in the "   if ($ok) {  " just add :

      $myfiles[$counter++] = $filename; // without extension ; $file is full path

and you are done with it.
     
0
fin51Author Commented:
Works great, thank you for your time and I understand
how it all works now.
0
VGRCommented:
of course you understand, because I didn't use ereg_replace(), strrchr() and such fancy things ;-)

Sorry for the time to reach a proper solution : I tried to adapt your script in stead of proposing mine; I thought it would be better to stick to your way of thinking-doing :D

regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.