Serving a php file to the user

Good afternoon everyone.

There is such a task: you need to give each user a unique php script that is generated automatically. Is there any way to organize this or is it necessary to ask the user to change the extension after receiving the file?

Tried to do it by sending headers. Here is the code:

$row = mysql_fetch_array($sql);
$filename = $row['filename'] . '.php';
$source_file = 'wp-block.txt';
$size = filesize($source_file);
$f=fopen($source_file, 'r');
header("HTTP/1.1 200 OK"); 
header("Connection: close"); 
header("Content-Type: application/octet-stream"); 
header("Accept-Ranges: bytes"); 
header("Content-Disposition: Attachment; filename=". $filename); 
header("Content-Length: ". $size);
echo fread($f, $size);

but in the end, when downloading, the extension is replaced with htm. Is it possible to do this and how?

Answer 1, authority 100%

Play with Content-Type. Try “text/plain”, “application/x-httpd-php”. The first one should work.

Answer 2, authority 100%

If you use Content-Type: text/plain then php code will be displayed in the browser.
If you need to give it to the download of the file, while setting the name of the downloaded file by default, then you need to use Content-Type
application/octet-stream or application/x-httpd-php


header("Content-Disposition: attachment; filename=" . urlencode("__.php"));

Answer 3

If it changes to html, it means that it returns the already completed one, and returns the html page, try specifying a different file type.

Answer 4

Hope this should help solve the problem:

$file = "{$_SERVER["DOCUMENT_ROOT"]}/any_file.php";
header("Content-type: application/force-download");
header("Content-length: " . filesize($file));

That is, if there is a page.php that contains this code, then when you visit it, you will be prompted to save the page.php file, the content of which will be the any_file.php file.