Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button To Download .txt File (PHP & HTML)

Tags:

html

php

onclick

Ok so I want to my users to be able to download a .txt file with their username on it.

I've checked everywhere, and so far I can't find what I want or if it's even possible.

To give you an example of what I'm trying to do, here is my code:

<button type="button">Download All Your Keys On A .txt
<?php
$file = 'logs/$session->username.txt';

if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");

$type = filetype($file); // Get a date and timestamp $today = date("F j, Y, g:i a"); $time = time(); // Send file headers header("Content-type: $type"); header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary"); 
header('Pragma: no-cache'); 
header('Expires: 0'); // Send the file contents.
set_time_limit(0); 
readfile($file);
?>
</button>

I'm trying to make it so that when you click the button, it force download's the .txt file configured with:

$file = 'logs/$session->username.txt';

Sorry if this is confusing and messy, but there's no other way for me to present this.

Thanks in advance!

like image 342
RepairServices Avatar asked Jun 26 '13 02:06

RepairServices


People also ask

How do I create a .TXT file in PHP?

PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

How do I force a download when I click on a link?

In most browsers, clicking on the link will open the file directly in the browser. But, if you add the download attribute to the link, it will tell the browser to download the file instead. The download attribute works in all modern browsers, including MS Edge, but not Internet Explorer.


1 Answers

Why don't you have this code in a separate file, say download.php:

<?php
    $file = "logs/{$session->username}.txt";

    if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");

    $type = filetype($file);
    // Get a date and timestamp
    $today = date("F j, Y, g:i a");
    $time = time();
    // Send file headers
    header("Content-type: $type");

    //** If you think header("Content-type: $type"); is giving you some problems,
    //** try header('Content-Type: application/octet-stream');

    //** Note filename= --- if using $_GET to get the $file, it needs to be "sanitized".
    //** I used the basename function to handle that... so it looks more like:
    //** header('Content-Disposition: attachment; filename=' . basename($_GET['mygetvar']));

    header("Content-Disposition: attachment;filename={$session->username}.txt");
    header("Content-Transfer-Encoding: binary"); 
    header('Pragma: no-cache'); 
    header('Expires: 0');
    // Send the file contents.
    set_time_limit(0);
    ob_clean();
    flush();
    readfile($file);

    //** If you are going to try and force download a file by opening a new tab via javascipt
    //** (In this code you would replace the onClick() event handler in the html
    //** button with onclick="window.open('www.someurl.com', '_blank');"
    //** - where 'www.someurl.com' is the url to the php page - I keep the file
    //** creation and download handling in the same file, and $_GET the file name
    // - bad practice? Probably, but I never claimed to be an expert),
    //** be sure to include exit(); in this part of the php... 
    //** otherwise leave exit(); out of the code.
    //** If you don't, it will likely break the code, based on my experience.

    //exit();
?>

Please note that you have to change the quotes to double quotes, as you use a variable inside the 's. So, to expand the variable, change the first line to:

$file = "logs/{$session->username}.txt";

Here I consider, $session->username, as the variable you are trying to refer.

And have this in the HTML:

<button type="button" onclick="location.href='download.php'">Download All Your Keys On A .txt</button>

And when you click on this button, it redirects to the download.php, that initiates a download of the txt file. As simple as that. But this requires you to have two files. And I don't understand the need for a button here. Why not just use a simple link like this?

<a href="download.php">Download All Your Keys On A .txt</a>

And if you need, you can style it using CSS.

like image 164
Praveen Kumar Purushothaman Avatar answered Oct 03 '22 17:10

Praveen Kumar Purushothaman