?
Solved

PHP File Listing

Posted on 2003-03-25
18
Medium Priority
?
262 Views
Last Modified: 2008-02-26
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
Comment
Question by:digitalbloke
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 5
18 Comments
 
LVL 15

Expert Comment

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

Author Comment

by:digitalbloke
ID: 8204819
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
 
LVL 1

Expert Comment

by:Kriek
ID: 8204873
VGR: He wants you to ereg_replace the file extension and only list the files in that directory rather than subdirectories "and" files.
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 1

Expert Comment

by:Kriek
ID: 8204892
VGR: He wants you to ereg_replace the file extension and only list the files in that directory rather than subdirectories "and" files.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8204950
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
 
LVL 1

Expert Comment

by:Kriek
ID: 8204967
Haha! Yes using spit() or explode() is much better =)
0
 

Author Comment

by:digitalbloke
ID: 8205000
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
 
LVL 15

Expert Comment

by:VGR
ID: 8205052
gimme five
0
 
LVL 1

Expert Comment

by:Kriek
ID: 8205060
<?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
 
LVL 15

Expert Comment

by:VGR
ID: 8205074
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
 
LVL 15

Accepted Solution

by:
VGR earned 300 total points
ID: 8205079
mine is shorter :D
0
 

Author Comment

by:digitalbloke
ID: 8205101
You are a STAR


A********


Many Thanks


Tom
0
 
LVL 1

Expert Comment

by:Kriek
ID: 8205104
VGR is awesome, hehe

I suggest you increase the points if you can.
0
 

Author Comment

by:digitalbloke
ID: 8205113
Thanks again!
0
 
LVL 15

Expert Comment

by:VGR
ID: 8205124
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
 
LVL 15

Expert Comment

by:VGR
ID: 8205169
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
 
LVL 15

Expert Comment

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

Author Comment

by:digitalbloke
ID: 8205201
ok many thanks i have learnt a lot!!!!


best regards to you too!
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 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