0

I can't get this download started, I followed what it is said here but I am not really sure I'm done things properly.

I have a DB, when I open the below page (from a link), I wish to be able to view the SQL content, and at the button click i wish to open the browser dialog box that will save the result of the SQL into a file.

From the menu, I click on "export", it opens this page (export.php):

<form id="exportConf" method="POST" action="exportConf.php">
<div id="exportPanel">
  <div class="row">
    <div class="col-md-8">
      <div class="row">
<?php
$query = "SELECT * from table";
$result = mysql_query($conn, $query);
echo "<input type=\"hidden\" name=\"export\" value=\"$result\"/>";
if ($result){
    echo "<table>";
    while ($row = mysql_fetch_row($result)){
    echo "<tr>";
    echo "<td>Name: </td><td>$row[0]</td>";
    echo "<td>Surname: </td><td>$row[1]</td>";
    echo "<td>Email: </td><td>$row[2]</td>";
    echo "<td>Phone: </td><td>$row[3]</td>";
    echo "</tr>";
    }
    echo "</table>";
} else {
    echo "Can't show the sql.";
}
?>
      </div>
    </div>
    <div class="col-md-4">
      <button type="submit" name="submit" class="btn btn-danger pull-right">Export to file</button>
    </div>
  </div>

  <div class="row clearfix">
  </div>

 </div>
</form>

The exportConf.php is this:

<?php
IF(isset($_POST['exportConf'])){
    //require_once('variables.php');
    $result = addslashes(strip_tags($_POST["export"]));
    /*$exportFile = "myfile.txt";
    header('Content-Type: application/download');
    header('Content-Disposition: attachment; filename=$exportFile');
    header("Content-Length: " . filesize($exportFile));

    $file = fopen($exportFile,"w+");
    echo fputs($file,$result);
    fclose($file);
    */
    echo "Test: $result";

    }
?>

I'm trying to print the value of $result but all I see is an empty white page.

I'm writing it on notepad and I do not have the DB ready, therefore no data BUT at least I expect it goes into the ELSE condition and add the $result inside the hidden input.

I should be able to only see Test: , in my white page. But I'm not. Someone can tell me what it is missing? And if I wrote the download part properly?

aPugLife
  • 989
  • 2
  • 14
  • 25

1 Answers1

0
<button type="submit" name="exportConf" class="btn btn-danger pull-right">Export to file</button>

Thanks to the comments, the "exportConf" was missing.

<?php
IF(isset($_POST['exportConf'])){
    require_once('config.php');
    require_once('variables.php');

    $handle = fopen("C:/file.txt", "w");
    fwrite($handle, $result);
    fclose($handle);

    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename('file.txt'));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize('file.txt'));
    readfile('file.txt');

    exit;
}

?>
aPugLife
  • 989
  • 2
  • 14
  • 25