By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,742 Members | 793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,742 IT Pros & Developers. It's quick & easy.

Script needed

P: n/a
Hi,

Im after a simple script to help sort my downloads.

Basically I have a downloads folder, and at the moment I have directory
browsing enabled so that i can download files.

What I want is to have a php script (index.php) that will parse the
files that it finds in downloads and create a nice page (using my
template and css). If it finds a directory in /downloads, then it
should create a 'group title' and all the files in that folder should
be grouped under that group title.

The important thing is that the ultimate links to the files themselves
need to clickable links to the files (ie, if you hover over the file
link the status bar shows blahblah.zip as the link), rather than to
another php file passing a variable (ie index.php?file=blah.zip). This
is because my server seems to stop downloads after 10mb unless the user
clicks on a file link.

Anyone know where I can get such a thing ?

Olly

Dec 15 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

You could install a file browser application, such as one of these, and
customize it to your needs:

www.ecosmear.com/relay/
www.rebelinblue.com/?fm

You could also write a script that uses opendir and readdir to print a
custom view of a directory. The script below does just this:

<?

//
// MyFileBrowser
//
// This class provides a simple file browser that lets
// users browse through a directory tree and access
// files as direct links. Customize the look as needed.
//
// Sample usage:
//
// $fb =& new MyFileBrowser('/physical/path', '/virtual/path');
// $fb->printDirectory();
//
// The first argument is the physical path of the directory
// you wish to browse. The second argument is virtual path of
// the directory on your web site.
//

class MyFileBrowser
{
var $sysRootPath;
var $virtualRootPath;

function MyFileBrowser($sysRootPath, $virtualRootPath = '.')
{
$this->sysRootPath = $sysRootPath;
$this->virtualRootPath = $virtualRootPath;
}

function printDirectory($path = null)
{
$buf = '';

if (is_null($path)) {
$path = isset($_GET['path']) ? $_GET['path'] : '/';
}
if (preg_match('/\.\./', $path)) {
trigger_error("Invalid path.", E_USER_ERROR);
return false;
}

$buf .= "<h1>File Browser</h1>";
$buf .= $this->printPath($path);

$parentPath = dirname($path);
if ($path != '/' && $path != '' && $path != '.') {
$dirPart = basename($parentPath);
if ($dirPart == '') {
$dirPart = 'Top';
}
$buf .= '<p style="font-size: 12px;">↑ <a href="' .
htmlentities($this->linkToDir($parentPath), ENT_QUOTES) .
'">Up to ' . htmlentities($dirPart) . '</a></p>';
}

$sysPath = $this->joinPath(array($this->sysRootPath, $path));
$dir = opendir($sysPath);

if (!$dir) {
$buf = false;
trigger_error("Failed to open \"$sysPath\".", E_USER_ERROR);
} else {
$entries = array();
while (($entry = readdir($dir)) !== false) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entries[] = $entry;
}

if (!sort($entries)) {
$buf = false;
trigger_error('sort', E_USER_ERROR);
} else {
$buf .= "<ul>";

foreach ($entries as $entry) {
$sysSubPath = $this->joinPath(array($sysPath, $entry));
if (is_dir($sysSubPath)) {
$buf .= $this->printSubDirectory(
$this->joinPath(array($path, $entry)));
} else {
$filePath = $this->linkToFile(
$this->joinPath(array($path, $entry)));
$buf .= "<li><a href=\"" .
htmlentities($filePath, ENT_QUOTES) .
"\">" . htmlentities($entry, ENT_QUOTES) . "</a></li>";
}
}

$buf .= "</ul>";
}

closedir($dir);
}

echo $buf;
}

function printPath($path)
{
$buf = '<p style="font-size: 20px; font-weight: bold;">Directory ';
$parts = preg_split('#[/\\\\]+#', $path, -1, PREG_SPLIT_NO_EMPTY);
array_unshift($parts, '/');

$curPath = '';
for ($i = 0; $i < count($parts); $i++) {
$part = $parts[$i];
$curPath = $this->joinPath(array($curPath, $part));
$url = $this->linkToDir($curPath);
$buf .= '<a href="' . htmlentities($url, ENT_QUOTES) . '" ' .
'style="font-size: 20px; font-weight: bold;">' .
htmlentities(($i == 0 ? 'Top' : $part), ENT_QUOTES) . '</a>';
if ($i < count($parts) - 1) {
$buf .= '<b/ </b>';
}
}

$buf .= '</p>';
return $buf;
}

function printSubDirectory($path)
{
$buf = '';

$sysPath = $this->joinPath(array($this->sysRootPath, $path));
$dir = opendir($sysPath);

if (!$dir) {
$buf = false;
trigger_error("Failed to open \"$sysPath\".", E_USER_ERROR);
} else {
$entries = array();
while (($entry = readdir($dir)) !== false) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entries[] = $entry;
}

if (!sort($entries)) {
$buf = false;
trigger_error('sort', E_USER_ERROR);
} else {
$buf .= "<p><li>";
$htmlName = htmlentities(basename($path), ENT_QUOTES);
$url = $this->linkToDir($path);
$buf .= "<p style=\"font-size: 20px; font-weight: bold;\">" .
"<a href=\"$url\">$htmlName</a></p>";
$buf .= "<ul>";

foreach ($entries as $entry) {
$htmlName = htmlentities($entry, ENT_QUOTES);
$sysSubPath = $this->joinPath(array($sysPath, $entry));
if (is_dir($sysSubPath)) {
$url = $this->linkToDir(
$this->joinPath(array($path, $entry)));
$buf .= "<li><a href=\"" . htmlentities($url, ENT_QUOTES) .
"\">$htmlName/</a></li>";
} else {
$filePath = $this->linkToFile(
$this->joinPath(array($path, $entry)));
$buf .= "<li><a href=\"" .
htmlentities($filePath, ENT_QUOTES) .
"\">$htmlName</a></li>";
}
}

$buf .= "</ul></li></p>";
}
}

closedir($dir);
return $buf;
}

function linkToFile($path)
{
return $this->joinPath(array($this->virtualRootPath,
preg_replace('#^[/\\\\]#', '', $path)));
}

function linkToDir($path)
{
$url = $_SERVER['PHP_SELF'];
$url = preg_replace('/([\?&])path=.*?(&|$)/', '\1', $url);
$c = $url[strlen($url) - 1];
if ($c != '?' && $c != '&') {
$url .= strrchr($url, '?') === false ? '?' : '&';
}
$url .= 'path=' . urlencode($path);
return $url;
}

function joinPath($parts)
{
$str = join('/', $parts);
$str = preg_replace('#/+#', '/', $str);
$str = preg_replace('#\\\\+#', '\\', $str);
return $str;
}
}

$fb =& new MyFileBrowser(dirname(__FILE__));
$fb->printDirectory();

?>

Regards,

John Peters

Oliver Marshall wrote:
Hi,

Im after a simple script to help sort my downloads.

Basically I have a downloads folder, and at the moment I have directory
browsing enabled so that i can download files.

What I want is to have a php script (index.php) that will parse the
files that it finds in downloads and create a nice page (using my
template and css). If it finds a directory in /downloads, then it
should create a 'group title' and all the files in that folder should
be grouped under that group title.

The important thing is that the ultimate links to the files themselves
need to clickable links to the files (ie, if you hover over the file
link the status bar shows blahblah.zip as the link), rather than to
another php file passing a variable (ie index.php?file=blah.zip). This
is because my server seems to stop downloads after 10mb unless the user
clicks on a file link.

Anyone know where I can get such a thing ?

Olly
Dec 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.