如何直接通过 PHP 将 Base64 字符串导出到服务器端的文件,而不将其保存在 Web 服务器中? [英] How to export a Base64 string to a file, on server side, without saving it in web server, directly by PHP?

查看:33
本文介绍了如何直接通过 PHP 将 Base64 字符串导出到服务器端的文件,而不将其保存在 Web 服务器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHPMySQL 进行编程.

I am using PHP and MySQL for my programming.

我存储了一些 Base64 字符串(从文件中编码).我知道 base64_decode(); 可以将 Base64 字符串解码为文件.

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

现在,我想通过 PHP 将这些 Base64 字符串解码为文件,而不将其保存在我的 网络服务器 中;但是,网络浏览器可以下载网络链接(http://example.com/save.php?fileid=23413).

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

在我的 PHP 页面中,http://example.com/save.php?fileid=23413,我做到了:

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

  1. 我可以使用 echo(); 来打印 Base64 字符串.所以,我的 MySQL 查询没有问题.
  2. 我已从我的 PHP 页面中删除 echo();.
  3. 我尝试使用 base64_decode();Base64 字符串解码为文件.我想当我导航到这个页面时,它会导出一个文件.但是,它返回一个页面.
  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.

<小时>

你能帮我用 PHPBase64 字符串解码为文件,而无需将其保存在我的 Web 服务器网络浏览器可以从网络链接 下载 (http://example.com/save.php?fileid=23413)?


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

推荐答案

在回显输出之前尝试向脚本添加标题

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

这篇关于如何直接通过 PHP 将 Base64 字符串导出到服务器端的文件,而不将其保存在 Web 服务器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆