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);
?> 

Link to comment
Share on other sites

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);
?> 

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.