Forum Moderators: coopster
The requested URL /joomla/docname.doc was not found on this server.
This is the code: its opensource too.
------
<?php // Download files script, just need to change the current_dir for each section below if reuse this code.
$current_dir = "$DOCUMENT_ROOT"."c:/wamp/www/joomla/Quality_Files/section1/"; //Put in second part, the directory - without a leading slash but with a trailing slash!
$dir = opendir($current_dir); // Open the sucker
echo ("<p><h1>List of available files:</h1></p><hr><br />");
while ($file = readdir($dir)) // while loop
{
$parts = explode(".", $file); // pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part
$extensions = end($parts); // set to we can see last file extensions
if (($extensions == "doc") OR ($extensions == "DOC") OR ($extensions == "docx") OR ($extensions == "DOCX") OR ($extensions == "pdf") OR ($extensions == "PFD")) // is extensions ext or EXT ?
echo "<li> <a href=\"$file\" target=\"_blank\"> $file </a><br />"; // If so, echo it out else do nothing cos it's not what we want
}
}
echo "<hr><br />";
closedir($dir); // Close the directory after we are done
?>
First, simplify this:
if (($extensions == "doc") OR ($extensions == "DOC") OR ($extensions == "docx") OR ($extensions == "DOCX") OR ($extensions == "pdf") OR ($extensions == "PFD"))
With this:
if (
preg_match('/docx*/i',$extensions) or
preg_match('/pf*df*/i',$extensions)
)
* means zero or more, making the previous character optional, so it would match doc or docx, pdf or pfd (but it would also match pd, so if this is a remote possibility, create two or's, one for pdf, one for pfd.)
i is the case insensitive modifier, so it would capture CaPs or LowErCase or both.
Second, you are correctly doing the full path to the file,
$current_dir = "$DOCUMENT_ROOT"."c:/wamp/www/joomla/Quality_Files/section1/";
But when you create the download link you're getting only the file name from "wherever you are" which is most likely domain root. So you need to append $file with the public http path to the file.
$current_dir = "$DOCUMENT_ROOT"."c:/wamp/www/joomla/Quality_Files/section1/";
$public_dir = '/joomla/Quality_Files/section1/';
.....
echo '<li> <a href="' . $public_dir . $file . '" target="_blank">' . $file . '</a></li>';
Third, it's pretty standard to not include trailing slashes in your setup variables which allows you a greater control over concatenating them, but this is just a matter of style and up to you. You may encounter a condition where that slash "gets in the way." So if you change this it would just be
$public_dir = '/joomla/Quality_Files/section1';
echo '<li> <a href="' . $public_dir . '/' . $file . '" target="_blank">' . $file . '</a></li>';
Last and probably most important - Doc files are not managed by browser "helper applications" so they will probably download without additional coding. But for PDF files, they will display in the browser. If you wish to force download of all files, you need to munge the content-type header. For PHP, it's a little different than just that.
echo '<li> <a href="yourscript.php?f=' . $file . '" target="_blank">' . $file . '</a></li>';
$filename = $_GET['f'];
$file = $current_dir . '/' . $filename;
if (is_file($file)) {
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
$size = filesize($file)+1;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
}
else { die("the DB file $filename does not exist."); }
<a href="download?f=file.ext">file.ext</a>
Then in the .htaccess for that directory,
RewriteEngine On
RewriteRule ^(download)$ /yourscript.php?$1&%{QUERY_STRING}
What is happening:
- if the request URL is download, save the request string in $1. Although that value is not needed here, you may have some validation or task that recognizes "download" in your script.
- Attach the query string - in this case f=file.ext - to the end of what's passed to yourscript.php.
yourscript.php should then be able to read in the directory as described above. A word of warning, security by obscurity is not really security at all, meaning, if this method is used to protect those files, they may be found anyway. Password protect that directory or disallow and requests for it, allowing only the PHP script to access it.
[edit:]
Actually this could be simplified.
<a href="download?file.ext">file.ext</a>
Set up yourscript.php to expect only a single parameter in the query string, you don't even need a key/value pair.
[/edit]
// start sending the headers
// The name of the download file is stored in $file
header("Cache-Control: public");
header("Pragma: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file');
header("Content-Transfer-Encoding: binary");
// start sending the file
// The .pdf source is stored on the server as $file.pdf
readfile($file);
==========================================
Code B:
For listing the contents of a directory with a link to download with the browser popup.
===========================================
$current_dir = "$DOCUMENT_ROOT"."/section1/"; //Put in second part, the directory - without a leading slash but with a trailing slash!
// declear html current directy pointer for the link.
$public_dir = "/section1/";
$dir = opendir($current_dir); // Open the sucker
echo ("<p><h1>List of available files:</h1></p><hr><br />");
while ($file = readdir($dir)) // while loop
{
$parts = explode(".", $file); // pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part
$extensions = end($parts); // set to we can see last file extensions
if (preg_match('/docx*/i',$extensions) or preg_match('/pf*df*/i',$extensions)) // is extensions ext or EXT ?
echo "<li><a href=".$public_dir."/". $file."> ". $file." </a></li>"; // If so, echo it out else do nothing cos it's not what we want
}
}
echo "<hr><br />";
closedir($dir);
===============================================
My question is:
How can i combine these so that when the pdf file is listed, it will download just like the .doc files.
If i try, it will automaticlly start a download without me even getting the option to select the file, or even seeing them listed. Since im using CMS its easier to use the same php file. Code B works perfectly for .doc.