从远程服务器使用file_get_contents后如何获取文件的mime类型 [英] How to get the mime type of a file after using file_get_contents from a remote server

查看:589
本文介绍了从远程服务器使用file_get_contents后如何获取文件的mime类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Alfresco的php中读取文件,然后将其输出到浏览器.唯一的问题是文件的模仿类型或扩展名.这是我正在使用的代码:

i'm reading a file from php from Alfresco and then outputting it to the browser. The only ptoblem is the mimetype or the extension of the file. This is the code i'm using:

<?php
ob_start();
//require_once("libs/FirePHPCore/fb.php");
require_once("libs/AlfrescoConnect.php");

$nomeFile = rawurldecode($_GET['nomeFile']);    
$urlDownload = $_GET['urlDownload'];
$fileDownloadUrl = AlfrescoConnect::$serverPath. $urlDownload . "&attach=true&alf_ticket=".AlfrescoConnect::getTiket();
fb($fileDownloadUrl);


$cnt = file_get_contents($fileDownloadUrl);


header("Content-type: Application/octet-stream");
header('Cache-Control: must-revalidate');
header('Content-disposition: attachment; filename=' .$nomeFile);
echo($cnt);
exit();

echo("Impossibile trovare il file");

我从get becausa接收文件的名称,我不知道如何从露天获取名称,但是我不得不以某种方式猜测这种mimetype.如果我在firsat角色中回显" $ cnt,则引用它是PDF的事实(例如,在屏幕上,我看到 %PDF-1.3%âãÏÓ2 0 obj<</长度3 0 R/过滤器/CCITTFaxDecode/DecodeParms<</K 0/列2480/行3508 >>/Type/XObject/子类型/Image/Width 2480/高度3508/BitsPerComponent 1/ColorSpace/DeviceGray >>流",因此必须有一种方法可以通过它从函数中获取mime_tipe.

I receive the name of the file from the get becausa i don't know how to get the name from alfresco, but i have to guess the mimetype somehow. if i "echo" $cnt in the firsat caracters there are references to the fact that it is a PDF (for example on screen i see "%PDF-1.3 %âãÏÓ 2 0 obj << /Length 3 0 R /Filter /CCITTFaxDecode /DecodeParms << /K 0 /Columns 2480 /Rows 3508 >> /Type /XObject /Subtype /Image /Width 2480 /Height 3508 /BitsPerComponent 1 /ColorSpace /DeviceGray >> stream" so there must be a way to get the mime_tipe from it with a function.

任何帮助都可以得到!

编辑.如果有人感兴趣,您可以使用该类从mime-type获取扩展名. http://www.ustrem.org/en/articles/mime-type-by-extension-zh-CN/

Edit. If anyone is intereste here is a class that you can use to get the extension from the mime-type. http://www.ustrem.org/en/articles/mime-type-by-extension-en/

推荐答案

使用cURL代替file_get_contents,则可以看到响应标头,希望该标头具有mime类型.

Use cURL instead of file_get_contents, then you can see the response header, which will hopefully have the mime type.

或者您可以尝试使用此 http://www.php.net/manual/zh-CN/ref.fileinfo.php 或此已弃用的函数 http://php.net/manual/zh-CN/function.mime-content-type.php

Or you could try using this http://www.php.net/manual/en/ref.fileinfo.php or this deprecated function http://php.net/manual/en/function.mime-content-type.php

这篇关于从远程服务器使用file_get_contents后如何获取文件的mime类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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