fpj
fpj

Reputation: 315

How to export a Base64 string to a file, on server side, without saving it in web server, directly by PHP?

I am using PHP and MySQL for my programming.


I have stored some Base64 strings (which are encoded from files). I know that base64_decode(); can decode Base64 strings to files.

Now, I want to decode these Base64 strings to files, by PHP, without saving it in my web server; but, web browser can download it from web link (http://example.com/save.php?fileid=23413).

In my PHP page, http://example.com/save.php?fileid=23413, I did:

  1. I can use echo(); for printing Base64 strings. So, there is no problem with my MySQL queries.
  2. I have delete echo(); from my PHP page.
  3. I tried to use base64_decode(); to decode Base64 strings to files. I want when I nagivate to this page, it will export a file. But, It returns an empty page.

Can you help me to decode Base64 strings to files, by PHP, without saving it in my web server, and web browser can download it from web link (http://example.com/save.php?fileid=23413)?

Upvotes: 2

Views: 2281

Answers (1)

Jaime
Jaime

Reputation: 1410

Try adding headers to the script before echoing the output

header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename= ".$file."");
echo base64_decode($file);

Upvotes: 3

Related Questions