This is the personal blog of Neil Ang. Simple and interesting technology articles written by a developer for developers. Feel free to comment on posts or link to this site. Constructive feedback is always welcomed.

PHP: Explore your server through a PHP file browser

Posted: 11 January 2007

In the past I've had to delete files from my server where the file permissions were set to apache and I could not override them because the host did not alllow terminal or shell access. So I quickly put together a PHP script that allowed me to navigate through the file system and unlink (or delete) files as the apache user. I haven't had to use the script for a while but I still think its pretty nifty to use when you want to explore a servers file system.

In the code below I've stripped out the unlink code (as it can be dangerous to have a page like that on your server) and just made it into a file explorer. Copy the code into a page and give it a go. If you're like me and have set your localhost as your homepage you can use it to quickly access files you have been working on. If anyone is interested in the version that allows you to unlink files, just send me an email and I will post it on the site.

<html>
<head>
  <title>PHP File Browser</title>
  <style type="text/css">
    .error{ color:red; font-weight:bold; }
  </style>
</head>
<body>

<?php
  // Explore the files via a web interface.   
  $script = basename(__FILE__); // the name of this script
  $path = !empty($_REQUEST['path']) ? $_REQUEST['path'] : dirname(__FILE__); // the path the script should access
  
  echo "<h1>Directory Browser</h1>";
  echo "<p>Browsing Location: {$path}</p>";
  
  $directories = array();
  $files = array();
  
  // Check we are focused on a dir
  if (is_dir($path)) {
    chdir($path); // Focus on the dir
   if ($handle = opendir('.')) {
      while (($item = readdir($handle)) !== false) {
        // Loop through current directory and divide files and directorys
        if(is_dir($item)){
          array_push($directories, realpath($item));
        }
        else
        {
          array_push($files, ($item));
        }
   }
   closedir($handle); // Close the directory handle
   }
    else {
      echo "<p class=\"error\">Directory handle could not be obtained.</p>";
    }
  }
  else
  {
    echo "<p class=\"error\">Path is not a directory</p>";
  }
  
  // There are now two arrays that contians the contents of the path.
  
  // List the directories as browsable navigation
  echo "<h2>Navigation</h2>";
  echo "<ul>";
  foreach( $directories as $directory ){
    echo ($directory != $path) ? "<li><a href=\"{$script}?path={$directory}\">{$directory}</a></li>" : "";
  }
  echo "</ul>";
  
  echo "<h2>Files</h2>";
  echo "<ul>";
  foreach( $files as $file ){
    // Comment the next line out if you wish see hidden files while browsing
    if(preg_match("/^\./", $file) || $file == $script): continue; endif; // This line will hide all invisible files.
    echo '<li><a href="' . basename($file) . '" target="_blank">' . $file . '</a></li>';
  }
  echo "</ul>";
?>

</body>
</html>

Your comments (subscribe)

Gravatar

richy 1 Sep 07 at 11:16am

Hi there, I am really interested in the unlink function, and how you would impliment this with the above script. I am no phper, but am starting to play around with some snippets to make life easier uploading and editing from remote locations.

Cheers, Richy

Gravatar

Tomasz 1 Sep 07 at 11:17am

Hello, I want to use the code you've made. But i got an error on line 61, that i corrected, and now it shows fine. But i still cannot change directories, i does not show an error, but when clicking on the links for the directories, it just redirects back to the dir where i put this script.

I hope you can help me out, please feel free to contact me through that email i used for this comment.

Cheers mate

Gravatar

SkechBoy 1 Sep 07 at 11:18am

heloooo mate and all this file browser is good but you should see this http://skechboy.no-ip.org/filemanager/ it is still under construction but when i will finish it is gona be a very good so if some one is interesed i will give you the source by by write me on skechboy (at) gmail (dot) com see ya

P.S. Sorry about the language the english version wil came soon :)

Gravatar

redivide 1 Nov 07 at 7:23am

Nice since I was about to write something like this but goog saves the day yet again :) thanks

Gravatar

SkechBoy 14 Jan 08 at 1:08am

Correction to the addres http://www.amphy.net/skechboy/filemanager/

Gravatar

Sherry 8 Mar 08 at 0:39am

Please provide the script with the unlink function.

Gravatar

SkechBoy 19 Apr 08 at 6:26am

Write me at my mail i will send you the source

Post a comment

Comment Guidelines

  • You can subscribe to the comments on this entry via RSS.
  • Have no more than 2 links, otherwise your comment will be flagged as spam.
  • Links are automagically generated.
  • <em>text</em> to make text italic.
  • <strong>text</strong> to make text bold.

JavaScript needs to be enabled to comment.