send a file to client

pradeep picture pradeep · Apr 10, 2009 · Viewed 45k times · Source

I want to write a text file in the server through Php, and have the client to download that file.

How would i do that?

Essentially the client should be able to download the file from the server.

Answer

Flavius picture Flavius · Apr 10, 2009

This is the best way to do it, supposing you don't want the user to see the real URL of the file.

<?php
  $filename="download.txt";
  header("Content-disposition: attachment;filename=$filename");
  readfile($filename);
?>

Additionally, you could protect your files with mod_access.