Good day to all.

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

Tried to do 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 by htm. Is it possible to do this and how?

  • Helped advice Sh4dow, everything works as it should. "text/plain" - used this. - Aferist
  • @Aferist Please click on the check mark to the left of the answer @ Sh4dow. - Nicolas Chabanovsky

4 answers 4

Play with Content-Type. Try "text / plain", "application / x-httpd-php". Must work first.

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

    and

     header("Content-Disposition: attachment; filename=" . urlencode("имя_вашего_файла.php")); 

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

      • There echo fread($f, $size); , not eval(fread($f, $size)); =) A php browser does not execute. - Sh4dow 6:02 pm
      • I'm talking about when accessing a file on a web server. - Andrey Arshinov

      I 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)); readfile("$file"); 

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