I want to download asynchronously a file from server. My files are located in 'files' directory - one directory higher than the content of the website.
In my code I have an ajax click event on div witch run asynchronously a php file named 'get_file.php' with some variables (GET method).
This is content of 'get_file.php'
<?php
$dir = '../../files/';
$file = $_GET['file'];
if (file_exists($dir . $file)) {
// only for test run a file via ajax
file_put_contents("test.tht", $dir . $file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dir . $file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($dir . $file));
ob_clean();
flush();
readfile($dir . $file);
exit;
}
?>
Code works corectly in part of transfer variables (because in test file.txt I can find a path and file name), but download doesn't start. What is wrong?