Solved

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

Posted on 2011-03-15
10
261 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:tomdenton
  • 5
  • 5
10 Comments
 
LVL 14

Expert Comment

by:EMB01
ID: 35140744
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.
0
 

Author Comment

by:tomdenton
ID: 35147460
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.
0
 
LVL 14

Expert Comment

by:EMB01
ID: 35147552
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.
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!

 

Author Comment

by:tomdenton
ID: 35149453
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 :)
0
 
LVL 14

Expert Comment

by:EMB01
ID: 35149637
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

0
 

Author Comment

by:tomdenton
ID: 35158428
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!
0
 
LVL 14

Expert Comment

by:EMB01
ID: 35159047
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

0
 

Author Comment

by:tomdenton
ID: 35159712
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
0
 
LVL 14

Accepted Solution

by:
EMB01 earned 500 total points
ID: 35159971
Well it was intended to use 'my-link.php' as a document that would distribute the file.  If you want to link directly to the file, then this code would work (attached).  The code is supposed to do this:

-  User views page and directory is displayed
-  User clicks directory
  -  Page reloads with URL parameter of directory name
  -  Directory contents are loaded
-  User clicks file
  -  Page loads with URL paramter of file name
  -  File is downloaded
-  Or...  User clicks file and user's browser is directed to the file's location
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='$dir/$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='$dir/$file'>$file</a><br />";
            
        }
        
      }

}

Open in new window

0
 

Author Closing Comment

by:tomdenton
ID: 35160711
This has been a great help, many thanks :-)
0

Featured Post

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!

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 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