Jump to content

display files inside a directory? with download button.


sayedsohail

Recommended Posts

Hi everyone,

 

My first version of simple code, display files and directories but when i tried only to display pdf files and subdirectories inside the given path, with pagination and downloadable option, everything went wrong.

 

ID  Download                  File Name  Type

1      CLICKABLE IMAGE      XYZ        PDF

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="$file"');

$dh = opendir('/usr/local/apache2/htdocs/');
$i=1;
echo "<table><tr><th>ID</th>Download</th><th>File Name</th><th>Type</th></tr>";
while ($file = readdir($dh))
{
echo "<tr><th>i++</th><th>".readfile('$file')."
</th><th>".$file."</th><th></th></tr>";
$i++;
}
echo "</table>";
closedir($dh);
?> 

Revised version1.1, i removed the download option now it displays the files and subdirectories, can someone help in adding a download button, where user can click the download link and the file starts downloading.

<?php

$dh = opendir('./');
$i=1;
echo "<table><tr><th>ID</th>Download</th><th>File Name</th><th>Type</th></tr>";
while ($file = readdir($dh))
{
echo "<tr><th>$i</th><th></th><th>".$file."</th><th></th></tr>";
$i++;
}
echo "</table>";
closedir($dh);
?> 

Million thanks for the help, Is there anyway, i could add funtionality to browse the subdirectories and a back button to parent directory?

 

Something like this, but this one is not showing anything:

<?php

# Original PHP code by Chirp Internet: www.chirp.com.au # Please acknowledge use of this code by including this header.
function getFileList($dir, $recurse=false) 
{ 
# array to hold return value 
$retval = array(); 
# add trailing slash if missing 
if(substr($dir, -1) != "/") $dir .= "/"; 
# open pointer to directory and read list of files 
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading"); 
while(false !== ($entry = $d->read())) { 
# skip hidden files 
if($entry[0] == ".") continue; 
if(is_dir("$dir$entry")) { 
$retval[] = array( 
"name" => "$dir$entry/", 
"type" => filetype("$dir$entry"), 
"size" => 0, 
"lastmod" => filemtime("$dir$entry") 
); 
if($recurse && is_readable("$dir$entry/")) 
{ $retval = array_merge($retval, getFileList("$dir$entry/", true)); 
} 
} 
elseif(is_readable("$dir$entry")) 
{ $retval[] = array( "name" => "$dir$entry", "type" => mime_content_type("$dir$entry"), "size" => filesize("$dir$entry"), "lastmod" => filemtime("$dir$entry") ); 
} 
} 
$d->close(); 
return $retval; 
}

# include subdirectories 
$dirlist = getFileList("./", true);

echo "<table>\n"; 
echo "<tr><th>Name</th><th>Type</th><th>Size</th><th>Last Mod.</th></tr>\n"; 
foreach($dirlist as $file) 
{ echo "<tr>\n"; 
echo "<td>{$file['name']}</td>\n"; 
echo "<td>{$file['type']}</td>\n"; 
echo "<td>{$file['size']}</td>\n"; 
echo "<td>" . date("r", $file['lastmod']) . "</td>\n"; echo "</tr>\n"; } 
echo "</table>\n\n";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.