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
digitalblokeAsked:
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:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

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
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
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.