Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

listing directory and filtering

Posted on 2003-03-26
13
Medium Priority
?
264 Views
Last Modified: 2006-11-17
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);
?>
0
Comment
Question by:fin51
  • 7
  • 6
13 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8212437
<?
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
 

Author Comment

by:fin51
ID: 8212589
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
 
LVL 15

Expert Comment

by:VGR
ID: 8212637
sorry, I miswrote the tests

suppress the NOT (!)

I'm stupid :D

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:VGR
ID: 8212679
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
 

Author Comment

by:fin51
ID: 8212680
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
 
LVL 15

Expert Comment

by:VGR
ID: 8212684
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
 

Author Comment

by:fin51
ID: 8212698
Sorry but what do you mean by supressing the NOT (!)
Just trying to learn and understand. This is new to mean.
0
 

Author Comment

by:fin51
ID: 8212744
Ok the last one works fine but it show files with  -days and -months but everything else is working perfect.
0
 

Author Comment

by:fin51
ID: 8212756
Ok the last one works fine but it show files with  -days and -months but everything else is working perfect.
0
 
LVL 15

Accepted Solution

by:
VGR earned 200 total points
ID: 8212820
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
 
LVL 15

Expert Comment

by:VGR
ID: 8212830
in the "   if ($ok) {  " just add :

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

and you are done with it.
     
0
 

Author Comment

by:fin51
ID: 8212861
Works great, thank you for your time and I understand
how it all works now.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8212886
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question