I have a PHP script for my website, which prints the dmesg
command out, and has a button to download the output.
Here is the script:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DMESG</title>
<script src="/admin/JS/JQuery.js"></script>
<script>
function download(){
var downloadString = encodeURIComponent($(".dmesg").html()).replace("%3Cbr%3E", "\n");
var a = document.body.appendChild(
document.createElement("a")
);
a.download = "DMESG.txt";
a.href = "data:text/plain," + downloadString;
a.click();
$("a").remove();
}
</script>
</head>
<body>
<center id="header">
<button onclick="download()">Download</button>
</center>
<hr />
<div class="dmesg">
<?php
exec("dmesg 2>&1", $dmesgOutput, $dmesgReturn);
if($dmesgReturn != "0") {
echo "Error!";
} else {
foreach ($dmesgOutput as $dmesgLine) {
echo $dmesgLine . "<br>";
}
}
?>
</div>
</body>
</html>
And upon clicking the Download
button, the contents of the file are here (notice the <br>
's).
I was wondering what I am doing wrong, and how can I fix this?
EDIT:
Here is the output of the encodeURIComponent()
variable.
`? – web-nomad Mar 22 '18 at 07:33