PDA

View Full Version : Attaching files for download


Nawizo
12-18-2010, 10:30 PM
Hello DC community. Does anyone know how i can attach a document(word doc, pdf, etc) to an html webpage so that it is downloadable. in short i would like to link a file so that when people clink the link, the browser starts to download it

DWcourse
12-18-2010, 11:34 PM
Upload the file to the server and link to it like any other file. Right+click (PC)/control+click (Mac) on the link brings up a dialog for the user to save the file to their computer. You should probably provide those instructions to users. Generally speaking pdfs are preferable to word documents since they are more universally openable (and avoid some other issues).

Nawizo
12-19-2010, 06:27 AM
DW it your answer gives me an option. thanks alot by the way. i have seen that on some webpages. but i would also like to know how to link the file so that when some clicks "download", the browser prompts them to either download, or view the file.

Nawizo
12-19-2010, 06:33 AM
DW it your answer gives me an option. thanks alot by the way. i have seen that on some webpages. but i would also like to know how to link the file so that when some clicks "download", the browser prompts them to either download, or view the file.

Corrosive
12-19-2010, 08:05 AM
DW it your answer gives me an option. thanks alot by the way. i have seen that on some webpages. but i would also like to know how to link the file so that when some clicks "download", the browser prompts them to either download, or view the file.

I think that is down to the browser.

gentleone
12-19-2010, 08:53 AM
With PHP you can force the browser to show the download dialog where the user can choose to open or save the file.

http://www.ryboe.com/tutorials/php-headers-force-download

Ricky55
12-19-2010, 06:47 PM
Yes you can but I can never get this working in Safari but why bother. Trying to change default browser behaviour never seems to make that much sense to me.

If you insist you can use this PHP


<?php

// block any attempt to the filesystem
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {

$filename = $_GET['file'];

} else {

$filename = NULL;

}

// define error message
$err = '<p style="color:#555454">Sorry for the inconvenience but the file you have requested is currently unavailable please <a href="../brochureDownloads.html">click here</a> to return to the page you were viewing.</p>';

if (!$filename) {

// if variable $filename is NULL or false display the message
echo $err;

} else {

// define the path to your download folder plus assign the file name
$path = '../pdfDownloads/brochures/'.$filename;

// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {

// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');

// open the file in binary read-only mode
// display the error messages if the file canīt be opened
$file = @ fopen($path, 'rb');

if ($file) {

// stream the file and exit the script when complete
fpassthru($file);
exit;

} else {

echo $err;

}

} else {

echo $err;

}

}
?>


Then in your link use


<a href="yourscript.php?file=fileToBeDownloaded.pdf">Download Here</a>

d a v e
12-20-2010, 07:00 AM
or zip it .