2

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!

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

0

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.

2 Comments

@user1856596 The server doesn't see it that way
Place the file where your php files are executing
0

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.