PhpSnip.com

User Stats

File and dir listing into tables, with download support

The function of this script is to distinguish wether the returned array is a file or directory, if it is a file, it generates a link to download the file, otherwise if it's a dir, it creates a link to list the files/dirs in that directory, works on windows and *nix platforms.

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 864

Source Code ( 119 lines )

<?php

/*
* The function of this script, is to distinguish wether the returned array is a file or directory,
* if it is a file, it generates a link to download the file, otherwise if it's a dir,
* it creates a link to list the files/dirs in that directory, works on windows and *nix platforms.
*
* v0.1: - initial release
* v0.2: - small fix for null directory
* v0.3: - small security fix
* v0.4: - changed some stuff
* v0.5: - security fix, update
*
* licence: gnu
* -- ds@breed.co.za
*/

/* config for the script */
define("DOWNLOAD_PATH", "/path/to/your/files"); /* path to your files, NB: no slash at the end */

/* start the script... no more editing from here on needed... */

/* get a list of the files + dirs and turn the list into an array */
function file_list($dir) {
  if (is_dir($dir)) {
    $fd = @opendir($dir);
    while (($part = @readdir($fd)) == true) {
      clearstatcache();
      if ($part != "." && $part != "..") {
        $file_array[] = $part;
      }
    }
    if ($fd == true) {
      closedir($fd);
    }
    if (is_array($file_array)) {
      natsort($file_array);
      return $file_array;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

/* function to convert to Mb, Kb and bytes */
function file_size($size) {
  if ($size > 1048576) { /* literal.float */
    return $re_sized = sprintf("%01.2f", $size / 1048576) . " Mb";
  } elseif ($size > 1024) {
    return $re_sized = sprintf("%01.2f", $size / 1024) . " Kb";
  } else {
    return $re_sized = $size . " bytes";
  }
}

/* prevent people from trying to view directories up */
function strip_junk($str) {
  return trim(str_replace("//", "/", str_replace("..", "", stripslashes(urldecode($str)))));
}

/* get a list of the files/dirs, put them into a table. */
function generate_file_list($path) {
  $final_path = strip_junk($path);
  $file_array = file_list(DOWNLOAD_PATH . "/$final_path/");
  echo "<b>$final_path/</b>n";
  echo "<br><br>nn";
  if ($file_array == false) { /* check if the dir is an array before we process it to foreach(); */
    echo "Directory emptyn";
  } else {
    echo "<table width="75%" border="0" cellspacing="0" cellpadding="0">n";
    echo "<tr><td><b>file</b></td><td><b>size</b></td></tr>n";
    foreach ($file_array as $file_name) {
      $is_file = DOWNLOAD_PATH . "/$final_path/$file_name";
      $file_size = file_size(filesize(DOWNLOAD_PATH . "/$final_path/$file_name"));
      if (is_file($is_file)) {
        print "<tr><td><a href="" .  $_SERVER["PHP_SELF"] . "?go=download&path=" . urlencode($final_path) . "&file=" . urlencode($file_name) . "">$file_name</a></td><td>" . $file_size . "</td></tr>n";
      } elseif (is_dir($is_file)) {
        print "<tr><td><a href="" . $_SERVER["PHP_SELF"] . "?go=list&path=" . urlencode($final_path) . "/" . urlencode($file_name) . "">$file_name/</a></td><td><dir></td></tr>n"; /* we don't need a size for a directory */
      }
    }
    echo "</table>n";
  }
}

/* allow the user to download the file... */
function do_download($path, $file) {
  $get_path = strip_junk($path);
  $get_file = strip_junk($file); /* fopen adds  to ' - so we strip 'em. */
  header("Content-Disposition: atachment; filename=" . $get_file);
  header("Content-Type: application/octet-stream");
  header("Content-Length: " . filesize(DOWNLOAD_PATH . "/$get_path/$get_file"));
  header("Cache-Control: no-cache, must-revalidate");
  header("Pragma: no-cache");
  header("Expires: 0");
  $fp = fopen(DOWNLOAD_PATH . "/$get_path/$get_file", "r");
  print fread($fp, filesize(DOWNLOAD_PATH . "/$get_path/$get_file"));
  fclose($fp);
  exit;
}

if (!isset($_GET[go]) || $_GET[go] == "dirlist") {
  generate_file_list(""); /* null, so we get a list for the root directory */
} elseif ($_GET[go] == "list" && isset($_GET[path])) {
  if (isset($_GET[path])) { /* if the path is null - it returns a list for the root directory */
    generate_file_list($_GET[path]); /* get a list for the path specified */
  } else {
    generate_file_list("");
  }
} elseif ($_GET[go] == "download") {
  if (isset($_GET[path]) && isset($_GET[file])) {
    do_download($_GET[path], $_GET[file]); /* download the file... */
  } else {
    echo "no file selected to download :)n";
  }
}

?>

Search

Subscribe

  Rss Feeds

Sponsors

Advertise