phpjquerypostforce-download

$_POST['index'] value echoes string but getting undefined error with readfile function


Here is the jQuery code I am using to send a random_num to force_dl.php, where random_num has the value 977926... my end goal is to force data977926.data to be downloaded instead of opened in a new window.

$.post( "../scripts/force_dl.php",{
    random_num: random_num
},
function( data ) {
    alert( data );
    var data_file = document.getElementById("data_file");
    data_file.href = "../scripts/force_dl.php";
});

and here is the HTML code being modified

<a id="data_file">Download data file</a>

When I have the following in force_dl.php,

<?php $file_name = "data" . $_POST['random_num'] . ".data";
echo "$file_name"; ?>

I see the file name in an alert,

data977926.data

and if I run the following in force_dl.php

header("Content-disposition: attachment; filename=data257667.data");
header("Content-type: text/plain");
readfile("../queries/gnuplot_tmp_files/data257667.data");

and click the HTML link with ID data_file, I am forced to download the data text file, as desired. The problem comes when I run the following. When I click the same data_file link I download a file named data.data!

$file_name = "data" . $_POST['random_num'] . ".data";
header("Content-disposition: attachment; filename=$file_name");
header("Content-type: text/plain");
readfile("../queries/gnuplot_tmp_files/$file_name");

Here is what is inside the data.data file,

<br />
<b>Notice</b>:  Undefined index: random_num in     <b>/var/www/html/msdb/scripts/force_dl.php</b> on line <b>2</b><br />
<br />
<b>Warning</b>:  readfile(../queries/gnuplot_tmp_files/data.data): failed to open stream: No such file or directory in     <b>/var/www/html/msdb/scripts/force_dl.php</b> on line <b>6</b><br />

Why is the index random_num undefined when I can echo $_POST['random_num'] just fine? Why is the downloaded file named data.data instead of data977926.data? Why do I see data977926.data when I echo $_POST['random_num'] but am getting different results than when I insert $_POST['random_num'] into the header and readfile functions than when I enter data977926.data? Thanks in advance.


Solution

  • In your JavaScript change

    data_file.href = "../scripts/force_dl.php";
    

    To this:

    data_file.href = "../scripts/force_dl.php?random_num=" + random_num;
    

    And in the php file for downloading change

    $file_name = "data" . $_POST['random_num'] . ".data";
    

    To this:

    $file_name = "data" . $_GET['random_num'] . ".data";
    

    This way you will pass the random number via GET method in the link to the php file, in your current code you aren't telling the php file in the second time what to get.

    Please note that if you want to use same php file for both of these actions you can check input values at first via isset function and do the right action based on them like this:

    <?php
    
    if(isset($_POST['random_num'])){
        echo 'data'.$_POST['random_num'].'.data';
    }elseif(isset($_GET['random_num'])){
        $file_name = 'data'.$_GET['random_num'].'.data';
        header('Content-disposition: attachment; filename='.$file_name);
        header('Content-type: text/plain');
        readfile('../queries/gnuplot_tmp_files/'.$file_name);   
    }
    
    ?>