Is there a simple way to create an empty temp .htm file on the server and send it to the user as download? I don't need to really create the file with fopen(), it's enough to just create it temporarily and send it to the user.
I need that for a website authentication. I found there is tmpfile() function within PHP, but it doesn't quite work for me yet. I am working on a Symfony project, maybe there are also a header function I don't know of.
All I need is:
$token = '12345';
$filename = $token . '.htm';
createatmpfile($filename);
header(send .htm file as download to the user);
Not sure how that works, but maybe my pseudocode above explains what I am looking for ..
EDITED:
That's how it works for Symfony2 (thanks Grad):
use Symfony\Component\HttpFoundation\Response;
Controller method:
public function generateAction() {
$response = new Response();
$response->headers->set('Content-Disposition', 'attachment; filename="'.$token.'.htm"');
return $response;
}
In plain PHP you could use to following line to force the user to download the response
header('Content-Disposition: attachment; filename="filename.htm"');
In Symfony you can use:
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="filename.htm"');
With this header set you can either just execute your controller (and let the view fill in the response), or use $contents = file_get_contents($path)
together with return $this->renderText($contents)
.
But depending on what you're trying to achieve: you don't have to create a tempfile to force the to download it.