-1

I have used file system to store the files, all files are either word or pdf files. how should i create their downloadable links on client side. Any help would be appreciated.

4 Answers4

1

you can download file using:

<?php
$file = 'filename.extension';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>
Priya jain
  • 753
  • 2
  • 5
  • 15
  • thanks for the help, but the code work for a specific file and i want to create downloadable links(on-click downloads) for downloading these files. – user3613632 May 14 '14 at 12:31
1

You can simply write the file path in the href attribute of the <a /> tag...

<a href="http://domain.com/files/pdf/readme.pdf"> Download Here </a>
sumgeek
  • 76
  • 1
  • 13
1
<?php 
if (isset($_GET['file'])) {
    $file = $_GET['file'];
    if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
        header('Content-Type: application/pdf');
        header("Content-Disposition: attachment; filename=\"$file\"");
        readfile($file);
    }
} else {
    header("HTTP/1.0 404 Not Found");
    echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
}

Try the above snippet and name it as download.php!

Once the above has been done, save the file and if needed upload to the server hosting your web page. Finally, once uploaded, all future .PDF links that you want to be downloaded instead of opened in the browser will need to point to download.php?file=example.pdf, where example.pdf is the name of the PDF you want for the user to download. Below is an example of what a full link could look like.enter code here

<a href="http://www.computerhope.com/download.php?file=example.pdf">Click here to download PDF</a>

0
<a href="http://example.com/files/doc/some_file.doc">Download</a>

This should do the job.

Tomáš Blatný
  • 892
  • 6
  • 20