Create text file and download without storing it on the server

0

How can I create a file at runtime of the script to download it, but without having to save it to the server directory?

Something like:

$dataOutput = "5s6a56sa565sa65a6s56sa565sa656sa565sa656as56sa556as5as";

//download
header('Content-Description: File Transfer'); ...
echo $dataOutput;
    
asked by anonymous 27.08.2017 / 23:34

1 answer

1

PHP

if($_SERVER['REQUEST_METHOD']=='POST'){

    $content = $_POST['meuTextarea'].PHP_EOL;

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    // Indica o nome do arquivo como será "baixado". Você pode modificar e colocar qualquer nome de arquivo     
    header('Content-disposition: attachment; filename=arquivo.txt');
    //header('Content-Length: '.strlen($content));
    //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    //header('Expires: 0');
    //header('Pragma: public');
    echo $content;
    exit;
}

HTML to create content

<form action="" method="post">
<textarea name="meuTextarea">5s6a56sa565sa65a6s56sa565sa656sa565sa656as56sa556as5as</textarea>
<p><input type="submit" value="Gerar/enviar"></p>
</form>
    
28.08.2017 / 00:29