I have a file in a server path and I would like to send it to the browser.
I tried something like this:
<?php
$filepath = '/var/tmp/apiRest/download/'.$rst;
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
exit();
}
If the variable $filepath
contains the name of the file in the form of variable $rst
, it does not download the file, but if I put the complete path by hand yes.
$filepath = '/var/tmp/apiRest/download/prueba.txt';
By doing a echo
of the variable $rst
, I'm fine with the name of the file but something fails to concatenate.
The call to PHP from the browser, using javascript, I do it in the following way:
function loadDoc(id){ //funcion que me carga un documento y lo guarda en download
var parametros = {
"id": id,
}
$.ajax({
data: parametros,
url: "php/PlnDir/load_doc.php",
type: "POST",
success: function (data) {
window.open("php/PlnDir/load_doc.php");
}
});
}