Problems opening a file from PHP in a new window

2

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");
        }
    });
}
    
asked by Lorenzo Martín 22.08.2018 в 17:31
source

2 answers

1

The problem you have is that you are requesting with a request XHR ( XMLHttpRequest ) the file using the POST method and sending the file identifier using the variable id :

$.ajax({
    data: {
        'id': id,
    },
    url: "php/PlnDir/load_doc.php",
    type: "POST",
    success: function (data) {
        window.open("php/PlnDir/load_doc.php");
    }
});

Until then everything is correct, but you can not trigger the download of a file by means of a request XHR if you do not add additional code (in a next edition I will explain how to do it).

To solve this problem, you have tried to open the PHP in a new window:

window.open("php/PlnDir/load_doc.php");

The problem is that the open window will load the PHP file using the GET normal method, without sending any data in the variable id (and neither does it per POST as expected), so this new request will fail and you will not find any file to download if you try to do the SQL search with $_POST['id'] .

To detect a failed submission of the parameter id you could have made the following check in the PHP file as follows:

<?php
if (isset($_POST['id']) === false) {
  die('Datos de "id" no recibidos');
}

There are two solutions to the problem. The simplest is to correctly send the parameter by POST to the new window. For this we can create a form whose field id is not visible and that it is loaded in _blank (new window):

<form action="php/PlnDir/load_doc.php" method="post"
    name="formulario" target="_blank">
  <input type="hidden" name="id" value="" />
</form>
<script>
function loadDoc(id){ //funcion que me carga un documento y lo guarda en download
  document.formulario.id.value = id;
  document.formulario.submit();
}
</script>

The most complex is to create a Blob from the data received by XHR , load them into a <a> tag and force them to click.

    
answered by 24.08.2018 / 22:20
source
-1

you could include the file using the include function

include '/var/tmp/apiRest/download/5730Turbo.txt';
  • Add, that the file, must be in the same folder (that the file to which you add the include) as I have put it.
answered by 22.08.2018 в 17:53