PHP file name variable boot file?

What is the correct syntax for this?

header('Content-Disposition: attachment; filename="{$_SESSION['name']} . '.txt'); 

This file works, albeit correctly, when the "view source" (HTML is not formatted, but contains the correct line breaks).

 <?php header("Content-type: text/html"); session_start(); if(file_exists("chat") && filesize("chat") > 0){ $handle = fopen("chat", "r"); $contents = fread($handle, filesize("chat")); fclose($handle); ob_start(); $download = strip_tags($contents, '<br>'); $processed = preg_replace('#<br\s*/?>#i', "\n", $download); echo $processed; $var = ob_get_clean(); echo $var; } ?> 

The variable $_SESSION['name'] has the value guest_158512 , and if you are viewing the source of the page I'm trying to save, it has all line breaks. How to save the current page as guest_158512.txt with the appropriate syntax?

+4
source share
3 answers

Your problem is that you tried using single quotes. Single quotes do not interpolate variables in PHP.

You also could not finish the line correctly, possibly because single quotes were added to it. Try:

 header('Content-Disposition: attachment; filename="'.$_SESSION['name'].'.txt"'); 

Kolink also indicates that you should use Content-Type: text/plain to work with text files other than HTML.

+6
source
 header("Content-Disposition: attachment; filename=\"".$_SESSION['name'].".txt\""); header("Content-Type: text/plain"); 

In the future, using the correct code editor, such as Notepad ++, you will see that your quotes are badly mismatched.

+1
source

You're close With single quotes, you can do the following:

 header('Content-Disposition: attachment; filename="' . $_SESSION['name'] . '.txt"'); 
0
source

Source: https://habr.com/ru/post/1396003/


All Articles