1

I'm trying to figure our how to download a text file made from content that's on the page. In my case it's just a chat log in plain text form. I have a button in the admin panel that you can use to view the chat log, and another one that should be used to download the said chat log.

An example of how the log is formatted:

2019-01-30 08:38:00 This is a chat log.

That's everything there is on the page.

The button I'm using to view the said chat log.

<td><a href="chatlog.php? 
user='.$rows['user'].'&reportedby='.$rows['reported_by'].'" class="btn btn- 
primary btn-sm">View messages</a></td>

 <?php session_start();
    include 'includes/db.php';  
    $sender = $_GET['user'];
    $receiver = $_GET['reportedby'];

    $query = "SELECT * FROM messages WHERE sender='$sender' AND receiver='$receiver'";

    $runq = mysqli_query($conn,$query);

    while($rows = mysqli_fetch_assoc($runq)) {
        echo($rows['date'] . " " . $rows['content']."<br/>");
    }

?>

JohnC
  • 161
  • 1
  • 9
  • 1
    Have you tried using the `Content-Disposition` header to tell the browser to download instead of display the file? – rickdenhaan Jan 31 '19 at 21:27
  • im not clear where the chat log text is stored? –  Jan 31 '19 at 21:27
  • 2
    It's not really clear what part of that process you're asking about. Can you explain in a bit more detail what you're having trouble with? – Don't Panic Jan 31 '19 at 21:32
  • Does the log text appear in the browser instead of downloading, or are you getting no output or incorrect output? Are there any errors? – Don't Panic Jan 31 '19 at 21:34
  • Sorry, I know I explained it pretty badly, I'll try again. Basiclly, all the messages are stored in the data base. When I view the chat log, it only shows the messages in a inbox from a user that was reported. The way I'm displaying the messages on the page is by just quering them from the database. I have added the said code to my main post. – JohnC Jan 31 '19 at 21:41
  • ok so just link to a page the queries the data base, outputs in the format you want and sets the page headers to force a download –  Jan 31 '19 at 21:46
  • 2
    Your sql query can cause Sql Injection. I recommend to change it – Omer Tekbiyik Jan 31 '19 at 22:09
  • I know it can cause SQL injections, but it's just a project for fun, it won't be going live. Thank you for the help! – JohnC Jan 31 '19 at 22:15

2 Answers2

1

With php code you could do like in the example below

    <?php 
        header("Content-type: text/plain");
        header("Content-Disposition: attachment; filename=savethis.txt");
        // Read your file and print it's content.
        // print "This is some text...\n";
        `enter code here`
    ?>
  • This works, but the problem I'm having is that it's formatted the wrong way. For example 2019-01-31 09:50:30 test
    29 Is it possible for it to actually go to the new line instead of printing
    , and I have no clue where the 29 is coming from.
    – JohnC Jan 31 '19 at 22:05
  • You can replace
    with \n
    – Arber Sokoli Jan 31 '19 at 22:08
0

You can archive this with Content-Type and Content-Disposition headers:

session_start();
include 'includes/db.php';
$sender = mysqli_real_escape_string(conn, $_GET['user']);
$receiver = mysqli_real_escape_string(conn, $_GET['reportedby']);

$query = "SELECT * FROM messages WHERE sender='$sender' AND receiver='$receiver'";

$runq = mysqli_query($conn, $query);

header('Content-Type: text/plain');
header("Content-Disposition: attachment; filename=chatlogs.txt");

while($rows = mysqli_fetch_assoc($runq)) {
    echo($rows['date'] . " " . $rows['content']."\r\n");
}

Also I replaced <br/> by actual line endings since it's a textfile, you may have to change it to \n if you're on a Unix system. And I added mysqli_real_escape_string to protect your query against SQL injection.

Jarzon
  • 480
  • 4
  • 13