简体   繁体   中英

Make temporary Files downloadable from Website

On a webservice I'm developing, a user should be able to download his data in a HTML file. This file contains everything (including images as base64).

Now to make the user download this, I would have to create the file and save it on my webserver. Afterwards, I'd have to delete it using a cronjob, because I can't figure out when the download is complete.

Is there another way? Would it be possible to download a file to the user which does not physically exist on my webserver, but gets somehow created temporarily?

Thank you for your help!

As far as the WWW is concerned, there is no such thing as a file. There are just HTTP resources.

The server might get the data from a file, it might not.

Just output the data for the file from your PHP program. (ie by putting it outside <?php and ?> or using echo or any other technique that causes PHP to output something).

You need to make sure you use the right Content-Type header, but since you are using HTML, that is text/html which is the default.

You can add a Content-Disposition header if you want the user to be prompted to save their download somewhere instead of rendering the downloaded HTML in the browser window.

header("Content-Disposition: attachment; filename='foo.html'");
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

From: http://php.net/manual/en/function.header.php

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