如何阅读Base64编码字符串作为一个文件,在服务器端,而不会被PHP保存在网络服务器,直接? [英] How to read a Base64 string as a file, on server side, without saving it in web server, directly by PHP?

查看:213
本文介绍了如何阅读Base64编码字符串作为一个文件,在服务器端,而不会被PHP保存在网络服务器,直接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多天前,我已经发布StackOverflow上的问题,并海梅已经回答了这个问题对我来说,<一个href=\"http://stackoverflow.com/questions/30585708/how-to-export-a-base64-string-to-a-file-on-server-side-without-saving-it-in-we\">at这里。

Many days ago, I have posted a question on StackOverflow, and Jaime has answered it for me, at here.

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

不过,我不想下载从数据库中的这些文件,我想它在Web浏览器中唯一的。

But, I don't want to download these file from database, I want to get it in web browser only.

例如,当我浏览到我的PHP页面( http://example.com/save ?.PHP FILEID = 23413 ),Web浏览器将读取这些输出中文件作为在计算机中的本地文件,正是:

For example, when I browse to my PHP page (http://example.com/save.php?fileid=23413), the web browser will read these ouput files as a local files in your computer, exactly:


  1. 如果输出文件是: JPG PNG GIF ... MP3 MP4 ... TXT HTML ...它可以处理,并在Web浏览器中播放。

  2. 如果其他输出文件类型,如: EXE DLL JAR ...我们必须通过Web浏览器下载

  3. 保存文件扩展名的文件时并不需要。

  4. 通过Web浏览器不要去code Base64编码字符串的文件(数据:[&LT;的MediaType&GT;] [; BASE64]&lt;数据&GT;

  1. If the output file is: JPG, PNG, GIF,... MP3, MP4,..., TXT, HTML... It can be handled and played in web browser.
  2. If other output file type, such as: EXE, DLL, JAR... It must be downloaded by web browser
  3. Saving files with file extensions is not neccessary.
  4. Don't decode Base64 strings to files by web browser (data:[<mediatype>][;base64],<data>).

我怎么能输出数据流的浏览器,并让浏览器处理它一样的服务器上的一个真正的文件,而不是总下载?

How can I output data stream to browser and have the browser handle it the same as a real file on the server, instead of always downloading?

我已经阅读计算器约Base64的其他问题,但目前还没有类似我的问题的问题。请仔细阅读我的问题来了解我的问题和其他问题之间的区别。

I have read other questions on StackOverflow about Base64, but there is no question similar to my question. Please read my question carefully to understand the differences between my question and other questions.

推荐答案

要获得MIME类型从数据流并将其设置为标题,可以使用内置的 finfo 类,如:

To get the mime-type from the data stream and set it to the header, use the built in finfo class, like:

$filecontents = base64_decode($file);

$finfo = new finfo(FILEINFO_MIME);
$file_mimetype = $finfo->buffer($filecontents);    

header("Content-type: $file_mimetype");
header("Content-disposition: inline; filename=$filename");
echo $filecontents;

这篇关于如何阅读Base64编码字符串作为一个文件,在服务器端,而不会被PHP保存在网络服务器,直接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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