I have a file download function in php that calls the download file, but the browser popup blocker is preventing it from opening.
The goal is to have an html form submit button which submits a form, edits an rtf file on the server based on the form inputs, then downloads the new file to the user.
Because I need php to run some code after the user clicks download and before the file is downloaded I couldn't get it to work any other way. I had to make php echo a link, then some js code that clicks that link.
The download function:
function download($path, $name){
if(!file_exists($path)){
echo $path." does not exist.";
}
else{
echo "
<a href='download.php?path={$path}&name={$name}' id='download'
target='download_frame' style='display:none;'>Download File</a>
<script>document.getElementById('download').click();</script>
";
}
}
download.php:
<?php
header("Content-type: application/doc");
basename($_GET["path"]));
$name = $_GET["name"];
header("Content-Disposition: attachment; filename='".$name.".doc");
header("Content-Transfer-Encoding: quoted_printable");
header('Pragma: no-cache');
header('Expires: 0');
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-Transfer-Encoding: binary');
header("Content-type: application/force-download");
header('Content-Type: application/octet-stream');
readfile($_GET["path"]);
You can do a header in place of echo and using JavaScript :
header ('location: download.php?path='. $path .'&name='. $name);
This will redirect to the download.php in same page and propose to download the file.