I've been wrestling around with this for awhile now. I am trying to make it so when a user click a link it will force a download. Here's my code so far:
<?php
function Download()
{
$fullpath = $_SERVER['DOCUMENT_ROOT']."front.dwg"; //Full path of document
$filename = "front.dwg"; //Document file nmae
$mm_type="application/octet-stream";
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($fullpath)) );
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary\n");
}
?>
<html>
<body>
<a href="#" onclick="<?php Download() ?>">Test</a>
</body>
</html>
Your going to need to pull out your function and save it as "download.php"
Then just have a link that goes to it:
<a href="download.php">
Download.php should look like this:
$fullpath = $_SERVER['DOCUMENT_ROOT']."front.dwg"; //Full path of document
$filename = "front.dwg"; //Document file nmae
$mm_type="application/octet-stream";
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($fullpath)) );
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary\n");
PHP is not a client-side function. You can't say onclick="somePHP"
.
You need a separate PHP script where you will force your download in, and simply link to that script.
In this script, you also need to actually output the file contents.
You need to set the appropriate calls to header
first. These have worked for me:
header('Pragma: public'); // required
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header("Content-disposition: attachment; filename=\"{$filename}\"");
header("Content-Type: {$mime}"); // also works with file extension
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);
die();
You can never FORCE a download. You can change the window location to the download file and it will prompt them, but if you could force downloads, there would be some major security issues.
First, your $fullpath
may not contain the data you expect, because you did not insert a forward slash ('/') before the filename. This is correct:
$fullpath = $_SERVER['DOCUMENT_ROOT']."/front.dwg";
You can check the contents of $fullpath
using error_log( "Fullpath: $fullpath" );
or echo "Fullpath: $fullpath\n"
.
Second, you need to make the PHP portion of your code a standalone script. You will not be able to embed PHP function calls in Javascript. All of your PHP executes before the Javascript runs, so you need a way to call the PHP separately.
Third, you should verify that the file exists and is readable using file_exists()
and is_readable()
. If PHP reports that the file does not exists, you should specify the full path. If it is not readable, you can change the file permissions to allow your PHP script to read it.