Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

mysql - 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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...