sayedsohail Posted September 28, 2007 Share Posted September 28, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71101-display-files-inside-a-directory-with-download-button/ Share on other sites More sharing options...
sayedsohail Posted September 28, 2007 Author Share Posted September 28, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71101-display-files-inside-a-directory-with-download-button/#findComment-357560 Share on other sites More sharing options...
sayedsohail Posted September 29, 2007 Author Share Posted September 29, 2007 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71101-display-files-inside-a-directory-with-download-button/#findComment-357629 Share on other sites More sharing options...
sayedsohail Posted September 29, 2007 Author Share Posted September 29, 2007 ??? Quote Link to comment https://forums.phpfreaks.com/topic/71101-display-files-inside-a-directory-with-download-button/#findComment-357642 Share on other sites More sharing options...
sayedsohail Posted September 29, 2007 Author Share Posted September 29, 2007 Quote Link to comment https://forums.phpfreaks.com/topic/71101-display-files-inside-a-directory-with-download-button/#findComment-357857 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.