我使用 PHP 和 MySQL 进行编程。
我存储了一些 Base64 字符串(从文件编码)。我知道 base64_decode();
可以将 Base64 字符串解码为文件。
现在,我想通过 PHP 将这些 Base64 字符串解码为文件,而不将其保存在我的网络服务器中;但是,网络浏览器可以从网络链接(http://example.com/save.php?fileid=23413)下载它。
在我的 PHP 页面中,http://example.com/save.php?fileid=23413 ,我做了:
- 我可以使用
echo();
打印 Base64 字符串。所以,我的 MySQL 查询没有问题。 - 我已从我的 PHP 页面删除
echo();
。 - 我尝试使用
base64_decode();
将 Base64 字符串解码为文件。我希望当我导航到此页面时,它将导出一个文件。但是,它返回一个空页面。
你能帮我通过PHP将Base64字符串解码为文件,而不将其保存在我的网络服务器中,并且网络浏览器可以从网络链接(http://example.com/save.php?fileid=23413)下载它吗?
最佳答案
在回显输出之前尝试向脚本添加 header
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename= ".$file."");
echo base64_decode($file);
关于php - 如何直接通过 PHP 将 Base64 字符串导出到服务器端的文件,而不将其保存在 Web 服务器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585708/