views:

138

answers:

1

I have list of images and I want a "Download" link along with every image so that user can download the image.

so can someone guide me How to Provide Download link for any file in php?

EDIT

I want a download panel to be displayed on clicking the download link I dont want to navigate to image to be displayed on the browser

+12  A: 

If you want to force a download, you can use something like the following:

<?php
    // Fetch the file info.
    $filePath = '/path/to/file/on/disk.jpg';

    if(file_exists($filePath)) {
        $fileName = basename($filePath);
        $fileSize = filesize($filePath);

        // Output headers.
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: ".$fileSize);
        header("Content-Disposition: attachment; filename=".$fileName);

        // Output file.
        readfile ($filePath);                   
        exit();
    }
    else {
        die('The provided file path is not valid.');
    }
?>

If you simply link to this script using a normal link the file will be downloaded.

Incidentally, the code snippet above needs to be executed at the start of a page (before any headers or HTML output had occurred.) Also take care if you decide to create an function based around this for downloading arbitrary files - you'll need to ensure that you prevent directory traversal (realpath is handy), only permit downloads from within a defined area, etc. if you're accepting input from a $_GET or $_POST.

middaparka
Exactly I want this, but how can i connect it with link like "Download" on clicking on which this script should get executed
OM The Eternity
Opps - good point. I'll update my answer.
middaparka
+1 Hey dude, it worked but the file downloaded found corrupted,i was not able to open the image..
OM The Eternity
@Om then look into the source of the downloaded file for any error messages.
Pekka
@Potter(Pekka) what are you talking about???? i didnt get any error msg...
OM The Eternity
@OM - just remembered, some browsers get pissy if you don't include a Cache-Control header. (Although that probably isn't the source of your error.) I'll updated the code again.
middaparka
@OM What @Pekka is saying is that if you open the downloaded file in a text editor (or simply comment out the header lines in the code) you'll see any PHP errors that occur, hence revealing the cause of the corruption. :-)
middaparka
guys i need this stuff to work for image files, document files of all sort... not only for textual readable files
OM The Eternity
@OM This code should work for *all* file types - you just need to find out what's causing the corruption.
middaparka
@OM Put simply - as a *temporary* measure, comment out the header lines and see if there are any errors being generated. Once these are fixed then un-comment the header lines and all should be well.
middaparka
hey geeks i commented three headers lines and I found these errors:: {ERRORS}Warning: filesize() [function.filesize]: stat failed for http://localhost/phptest/nimbuz.jpeg in C:\wamp\www\download.php on line 7Warning: readfile(http://localhost/phptest/nimbuz.jpeg) [function.readfile]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\wamp\www\download.php on line 15{/ERRORS}
OM The Eternity
@OM You need to: a) Adjust your attitude a bit. b) Provide a valid path to the file on disk. :-)
middaparka
@middaparka a) dont take it otherwise, i used to kidd wid Pekka all the time.. I apologize if you felt bad for the fellow SO, b) I have provided path as "$filePath = 'C:/wamp/www/phptest/'.$_REQUEST['image'];"
OM The Eternity
@middaparka I m still getting corrupted image downloaded.. plss help..
OM The Eternity
@OM - If you echo out the full path ($filePath) in the PHP script what is it? (And does that same path work in Windows explorer.) In essence, the errors are both stating that the file can't be found. I've also updated my code a bit.
middaparka
@middaparka - i am still facing the corrupted file downloaded...
OM The Eternity
@ALL - If on a windows system, try replacing the forward slash (/) with a backslash (\)
webfac
@OM - If my previous comment doesn't illustrate the problem, then I'm afraid I don't know what will. :-)
middaparka
@webfac - i still get downloaded the curropt image file
OM The Eternity