如何阅读Base64编码字符串作为一个文件,在服务器端,而不会被PHP保存在网络服务器,直接?
许多天前,我已经发布StackOverflow上的问题,并海梅已经回答了这个问题对我来说,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:
- 如果输出文件是:
JPG
,PNG
,GIF
...MP3
,MP4
...TXT
,HTML
...它可以处理,并在Web浏览器中播放。 - 如果其他输出文件类型,如:
EXE
,DLL
,JAR
...我们必须通过Web浏览器下载 -
保存文件扩展名的文件
时并不需要。 - 通过Web浏览器不要去code Base64编码字符串的文件(
数据:[<的MediaType>] [; BASE64]<数据>
)
- If the output file is:
JPG
,PNG
,GIF
,...MP3
,MP4
,...,TXT
,HTML
... It can be handled and played in web browser. - If other output file type, such as:
EXE
,DLL
,JAR
... It must be downloaded by web browser -
Saving files with file extensions
is not neccessary. - 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;