简体   繁体   中英

How to force download web content to server using php

Let's say I open this this page http://localhost/test/index.php and give me this output:

<p>test</p>
<p>second test</p>

The page header is plain text so it will not display the html formatted

header("Content-Type: plain/text");

Question is how do I automatically grab the plain text content and write it into text file and save it in the web server. For example is http://localhost/test/grab/text1.txt

"EDIT"

"Save the files contents to somewhere on the webserver, not ask the client to save it"...

Assuming you've phrased your question correctly, and you actually want to save on server instead of client.

You could use output buffering to capture the output and then save it in a file of your choosing

http://www.php.net/manual/en/function.ob-start.php

An example from the manual page (changed to save the file)

<?php

function callback($buffer)
{
  //save the content
  $filename = 'grab/test.txt';
  file_put_contents($filename, $buffer);
}

ob_start("callback");

?>

<html>
 <body>
   <p>test</p>
   <p>second test</p>
 </body>
</html>
<?php

ob_end_flush();

?>

"Save it in the web server" ? Do you mean the client should be prompted to save the response as a file on her local system? In that case use the Content-Disposition header:

header('Content-Disposition: attachment; filename="text1.txt"');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM