Link to home
Start Free TrialLog in
Avatar of tomdenton
tomdenton

asked on

How can i navigate a simple folder structure and display thier contents in PHP?

Hi, I would like to be able to display folders to say three levels at the most using PHP on a website so that my users can find a file they need and download it. I searched and found some useful stuff and edited it a bit as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>ATT Training Files</title>
<style type="text/css">
<!--
.style1 {color: #990000}
-->
</style>
</head>

<body>
<h1 class="style1">ATT Hairdressing support documents </h1>
<p class="style1">Please click on one of the links below to download or open the file:</p>
<p>
  <?php
$Directory = "Docs";
$Dir = opendir($Directory);
while ($File = readdir($Dir))
{
   $extension = pathinfo($File,PATHINFO_EXTENSION);
   $name      = pathinfo($File,PATHINFO_BASENAME);  
   if ($extension == 'jpg' || $extension == 'docx' || $extension == 'doc' )
   {
         echo '<a href="'.$Directory.'/'.str_replace('+','%20',urlencode($File)).'">'.$name.'</a></br>';
   }
}
closedir($Dir);
?>
</p>
</body>
</html>

This is at: http://www.atttraining.com/vle/files_repository/index.php 
This code displays the files in a subfolder called /docs as specified but ideally I would like it to first display the folders for the user to choose which one they want.
I can make it look pretty later!

Any help much appreciated
Tom
Avatar of EMB01
EMB01
Flag of United States of America image

You can do this by applying the following code:

<?php
$dir    = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);

print_r($files1);
print_r($files2);
?>

Ref.  http://www.php.net/manual/en/function.scandir.php

You can then make the directories clickable like this:

// loop through directory
foreach ($files1 as $file) {

  // check if it's a file or folder
  if (is_dir($file)) {

    // output directory link
    echo "<a href='my-link.php?folder=$file'>$file</a><br />";

  } else {

    // output file link
    echo "<a href='my-link.php?file=$file'>$file</a><br />";

  }

}

There are other ways to do this, too.  This is just a basic example.

Please let me know if you need any help.
Avatar of tomdenton
tomdenton

ASKER

Thanks that's a big help - and the php manual link is usefull too.

I am very new to PHP so please bear with me! Using the following shows me files and folders and makes then clickable:

<?php
$dir    = './docs';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);
// loop through directory
foreach ($files1 as $file) {
  // check if it's a file or folder
  if (is_dir($file)) {
    // output directory link
    echo "<a href='my-link.php?folder=$file'>$file</a><br />";
  } else {
    // output file link
    echo "<a href='my-link.php?file=$file'>$file</a><br />";
  }
}
?>

The final thing I need to do is when clicking one of the folder links, I would like it to open and list its contents in the same way.
You could do it like this:

if (isset($_GET['folder']) {

      // output directory
      $dir    = addslashes($_GET['folder']);
      $files1 = scandir($dir);
      $files2 = scandir($dir, 1);
      
      // loop through directory
      foreach ($files1 as $file) {
      
        // check if it's a file or folder
        if (is_dir($file)) {
        
            // output directory link
            echo "<a href='my-link.php?folder=$file'>$file</a><br />";
            
        } else {
        
            // output file link
            echo "<a href='my-link.php?file=$file'>$file</a><br />";
            
        }
        
      }

} else if (isset($_GET['file'])) {

      // output file
      $file = addslahes($_GET['file']);
      $path = "this.is.my.domain.com/files/";
      header("Location: $path.$file");
      
}

You could also do it the cool way with javascript by making the link display a div containing the next folders contents, but that would be slightly more complicated.  You can open a new question for it though, and I could assist you.
I get the error: Parse error: syntax error, unexpected '{' .... line...

on the line when the IF starts. The exact code i am trying to run is:

if (isset($_GET['folder']) {
      // output directory
      $dir    = addslashes($_GET['folder']);
      $files1 = scandir($dir);
      $files2 = scandir($dir, 1);
      // loop through directory
      foreach ($files1 as $file) {
        // check if it's a file or folder
        if (is_dir($file)) {
            // output directory link
            echo "<a href='my-link.php?folder=$file'>$file</a><br />";
        } else {
            // output file link
            echo "<a href='my-link.php?file=$file'>$file</a><br />";
        }
      }
} else if (isset($_GET['file'])) {
      // output file
      $file = addslashes($_GET['file']);
      $path = "http://www.atttraining.com/vle/files_repository/docs/";
      header("Location: $path.$file");
        }
?>

I can't see any missing curly brackets but maybe i have missed something else?? Sorry if i am not seeing something obvious here!
Thanks for the javascript offer - i will come back on that another time as i would like to learn a bit more PHP first :)
Sorry, forgot on parenthesis.  Try this one.  

P.S.  I made a change so that, by default, it will show your directory, unless otherwise specified.

Let me know how it works; additional adjustments may be required.
if (isset($_GET['folder'])) {

      // output directory
      $dir    = addslashes($_GET['folder']);
      $files1 = scandir($dir);
      $files2 = scandir($dir, 1);
      
      // loop through directory
      foreach ($files1 as $file) {
      
        // check if it's a file or folder
        if (is_dir($file)) {
        
            // output directory link
            echo "<a href='my-link.php?folder=$file'>$file</a><br />";
            
        } else {
        
            // output file link
            echo "<a href='my-link.php?file=$file'>$file</a><br />";
            
        }
        
      }

} else if (isset($_GET['file'])) {

      // output file
      $file = addslahes($_GET['file']);
      $path = "this.is.my.domain.com/docs/";
      header("Location: $path.$file");
      
} else {

	  // show default directory
	  $dir    = "./docs";
      $files1 = scandir($dir);
      $files2 = scandir($dir, 1);
      
      // loop through directory
      foreach ($files1 as $file) {
      
        // check if it's a file or folder
        if (is_dir($file)) {
        
            // output directory link
            echo "<a href='my-link.php?folder=$file'>$file</a><br />";
            
        } else {
        
            // output file link
            echo "<a href='my-link.php?file=$file'>$file</a><br />";
            
        }
        
      }

}

Open in new window

The latest version lists the files and folders in my default folder (/docs) ok, but the hyperlink is created by line 20 for both the file and folders (I just added XXXX the the href to prove this)

The path of these links is to where I run index.php ie it does not include the /docs

Also, the link includes the text: "my-link.php?file=" should i just delete this to make the links work or should i also have a file my-link.php?

Learning all the time here... thanks!
You're welcome.  I added the link to files because I thought you would want that.  If you don't want files to be linked, then use this code (attached).  Please ask any questions, I'm happy to help!
if (isset($_GET['folder'])) {

      // output directory
      $dir    = addslashes($_GET['folder']);
      $files1 = scandir($dir);
      $files2 = scandir($dir, 1);
      
      // loop through directory
      foreach ($files1 as $file) {
      
        // check if it's a file or folder
        if (is_dir($file)) {
        
            // output directory link
            echo "<a href='my-link.php?folder=$file'>$file</a><br />";
            
        } else {
        
            // output file link
            echo "$file<br />";
            
        }
        
      }

} else if (isset($_GET['file'])) {

      // output file
      $file = addslahes($_GET['file']);
      $path = "this.is.my.domain.com/docs/";
      header("Location: $path.$file");
      
} else {

	  // show default directory
	  $dir    = "./docs";
      $files1 = scandir($dir);
      $files2 = scandir($dir, 1);
      
      // loop through directory
      foreach ($files1 as $file) {
      
        // check if it's a file or folder
        if (is_dir($file)) {
        
            // output directory link
            echo "<a href='my-link.php?folder=$file'>$file</a><br />";
            
        } else {
        
            // output file link
            echo "$file<br />";
            
        }
        
      }

}

Open in new window

I do want the files to hyperlink, what i meant was that the line you just changed is what seemed to be making the links on ALL the items ie folders or files, in other words the folders do not seem to be being recognised as folders as it is line 52 above that was making the link not 47 or even line 15.

Ie everything seems to end up inside the final 'else' statement at line 49??

i added:
     echo "Hello World";
on line 2 but it never shows... which suggests that the IF on line 1 is never met?

The output at the moment is:
.
..
folder1
folder2
update.docx

and by editing the line that makes the link i can make the document downloadable
ASKER CERTIFIED SOLUTION
Avatar of EMB01
EMB01
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This has been a great help, many thanks :-)