How to export a Base64 string to a file, on server side, without saving it in web server, directly by PHP?(如何直接通过 PHP 将 Base64 字符串导出到服务器端的文件,而不将其保存在 Web 服务器中?)
问题描述
我正在使用 PHP 和 MySQL 进行编程.
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:
- 我可以使用
echo();来打印 Base64 字符串.所以,我的 MySQL 查询没有问题. - 我已从我的 PHP 页面中删除
echo();. - 我尝试使用
base64_decode();将 Base64 字符串解码为文件.我想当我导航到这个页面时,它会导出一个文件.但是,它返回一个空页面.
- I can use
echo();for printing Base64 strings. So, there is no problem with my MySQL queries. - I have delete
echo();from my PHP page. - 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.
<小时>
你能帮我用 PHP 将 Base64 字符串解码为文件,而无需将其保存在我的 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 服务器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何直接通过 PHP 将 Base64 字符串导出到服务器端的文件,而不将其保存在 Web 服务器中?
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
