user1856596
user1856596

Reputation: 7233

PHP: Download a file on button click?

I created a form, which, when a contained button is clicked, should open a download dialog to download a certain file. The file is placed on the same server.

I tried:

header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=" . $file . '"'); 

Where $file is a local path + the file name, for example c:\mypath\myfile.xls. This does not work though. It offers me a file, but its not a valid file. How else could I do that?

Note: I wrote c:\ because its still on my local machine for testing.

Thanks!

Upvotes: 2

Views: 17708

Answers (4)

Somwang Souksavatd
Somwang Souksavatd

Reputation: 5085

Try this

header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
die(file_get_contents($file));

I think file_get_contents() function is no longer work with PHP 5.0.3

Upvotes: 3

Ravinder Singh
Ravinder Singh

Reputation: 3133

Try this :

$path = "http://www.example.com/files/";
$filename = "abc.gif";

header("Content-Type:image/gif");
header("Content-Disposition: attachment; filename=".$filename);
header("Cache-control: private");
header('X-Sendfile: '.$path);
readfile($path);
exit;

Upvotes: 1

ka_lin
ka_lin

Reputation: 9432

Path must be refered from the site root...move the file ex: script path : C:/wamp/www/test.php file C:/script.js then:

if(file_exists('../../user.js'))
{
    echo "OK";
}

Still a bad ideea..

Upvotes: 0

Prasanth Bendra
Prasanth Bendra

Reputation: 32710

PHP runs in server side, you can not download the files in clients machine.

Upload the files to server and then give that path for download.

Upvotes: 0

Related Questions