How to force the download of a text file?

Asked

Viewed 1,777 times

0

How to Force Download a Text File?

I have this code but it is not downloaded, when I click on the link its contents is displayed by the browser.

<?php

include "conecte.php";

$querymail = mysql_query("select cod,name from usuarios");
fopen("listar.txt", "w+");
while($data = mysql_fetch_array($querymail)) {
    $log = str_pad($data[0], 30, " ", STR_PAD_RIGHT);
    $log2 = str_pad($data[1], 30, "0", STR_PAD_RIGHT);
    if (!$savelog = fopen('listar.txt', "a")){
        exit;
    }        
    if (!fwrite($savelog, $log . $log2. "\r\n")){
        exit; fclose($savelog); 
    }
}
?>


<a href="listar.txt">Clique aqui para baixar</a>
  • 1

    What is your doubt?

  • Colleague, the file will be created wherever your PHP is. To download just click on save as there at the end. What is the question?

  • Do I need to download the generated file into a folder on the pc when clicking (Click here to download) ? As is it only opens in the browser...

  • The server has no knowledge of client folders, so the file goes to the default download folder set by the browser.

  • I have already noticed in the download default folder and it is not listed...

  • The text is displayed on a page when you click on the link that? the correct one would be to download??

Show 1 more comment

2 answers

2


Create a new file (download.php) in it set the header to be sent or whatever format the browser should interpret the information open the file and send to the client.

download php.

header('Content-disposition: attachment; filename=lista.txt');
header('Content-type: text/plain');
$str = file_get_contents('log/lista.txt');
echo $str;

Calling for:

<a href="download.php">Clique aqui para baixar</a>

A more optimized option, cited by Wallace Maxters is to trade file_get_contents() for readfile()

header('Content-disposition: attachment; filename=lista.txt');
header('Content-type: text/plain');
readfile('log/lista.txt');

Related:

PHP readfile vs. file_get_contents

  • Worked perfectly!

  • Rray, do you mind a suggestion? Use file_get_contents followed by a echo is a bit of memory waste (depending on the file size). file_get_contents reads the file and returns its string, and in your case, saved in a variable. In this case, use readfile that already read the file printing the content immediately, without storing anything in memory

  • @Wallacemaxters readfile(file_get_contents('log/lista.txt')); ?

  • No, it’s like this: readfile('log/lista.txt')

  • @Wallacemaxters that :D, fuck q php documentation ta blocked here kkk wtf O.o?

  • @Wallacemaxters LOL looks at editing

  • Linka me too, kkkkk

Show 3 more comments

2

The question has been answered, but there is another solution or complement. You can use the attribute download tag a:

<a href="LocalDoArquivo" download="NomeDoArquivo"> Texto </a>

Example:

<a href="/questions/122672/como-for%c3%a7ar-o-download-de-um-arquivo-de-texto" download="Baixou ao invés de acessar"> Baixar </a>

This way you will download the HTML from this page, instead of accessing normally.

This is new, inserted in HTML 5!

Support:

Chrome: 14.0+
Internet Explorer: 13.0+
Mozilla: 20.0+
Safari: Não suportado
Opera: 15.0+
  • +1, I think it’s a cool idea, if it’s in the case of someone who uses only the HTML , for example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.