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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

PHP File Listing

Dear Experts

I currently have a very simple php script that lists files and directories in a directory. I know little php but have made minor changes. I would like to be able to stop the listing including directories and to remove the file extenctions from the end of the listing. I currently have the following code

<?php

 $directory = "eput/";
 if($dir_handle = opendir($directory)){
   while($file = readdir($dir_handle))
   {
     if($file !== "." && $file !== ".."){
       echo "<a href=".$directory.$file."php".">".$file."</a><br>";
     }
   }
 }
 closedir($dir_handle);
?>

Could someone please help me!!!


Many Thanks
0
digitalbloke
Asked:
digitalbloke
  • 8
  • 5
  • 5
1 Solution
 
VGRCommented:
huh ?
to get rid of the extensions in the visible part is no problem (if it's what you want)

BUT i don't understand "to stop the listing including directories" !

IMHO it's 30 seconds of my time, but I need to understand first what you mean 8-)
0
 
digitalblokeAuthor Commented:
Many thanks to your responce.

I only want files to be listed on the screen not directories as well. and yes you got the extenction bit right.


Many thanks
0
 
KriekCommented:
VGR: He wants you to ereg_replace the file extension and only list the files in that directory rather than subdirectories "and" files.
0
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.

 
KriekCommented:
VGR: He wants you to ereg_replace the file extension and only list the files in that directory rather than subdirectories "and" files.
0
 
VGRCommented:
no Kriek :D
YOU would have ereg-ed, I will program 8-)

<?php

$directory = "eput/";
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
 
KriekCommented:
Haha! Yes using spit() or explode() is much better =)
0
 
digitalblokeAuthor Commented:
I now get an error message

Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in /web/sites/164/tomwelch/www.tomwelch.f2s.com/eput/nav2.php on line 16

i have hosted the page at http://www.eput.co.uk/nav2.php if that helps
0
 
VGRCommented:
gimme five
0
 
KriekCommented:
<?php
$directory = "Gallery";
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
 
VGRCommented:
yes, a stupid quotes problem (I half-fixed your echo line)
and my fault in control structures.

This works right out :

<?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
 
VGRCommented:
mine is shorter :D
0
 
digitalblokeAuthor Commented:
You are a STAR


A********


Many Thanks


Tom
0
 
KriekCommented:
VGR is awesome, hehe

I suggest you increase the points if you can.
0
 
digitalblokeAuthor Commented:
Thanks again!
0
 
VGRCommented:
do you really like the $filename = current(explode('.', basename($file))); ?

I'm not even sure that this works if the filename doesn't contain a dot, as current will return FALSe in this case.
0
 
VGRCommented:
just a final note for you digitalbloke :

when you include variable output into a printed/echoed string, you have to care fot the quotes.

If you use single quotes, the string is sincidered a string constant and is NOT parsed for variable replacement
If you use double quotes, it will be.
Also, you DON'T NEED to "interrupt" your string to concanenate (with the dot operator) variables' valuies.

Thus correct string echo is :

echo "this is a string with $number variables in it<BR>";
echo 'this is a string with $number variables in it<BR>';

the first will display "this is a string with 0 variables in it"
the second will display "this is a string with $number $variables in it<BR>"


0
 
VGRCommented:
the only case where you need to interrupt a string to be displayed to concatenate things to it is, à part from the trivial case where you call a function, for ARRAYS.

You'll have to write :

echo "this is a string with ".$thearray[$theindex]."variables in it<BR>";

best regards
0
 
digitalblokeAuthor Commented:
ok many thanks i have learnt a lot!!!!


best regards to you too!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now