[英]How to get the content-type of a file in PHP?
我正在使用PHP发送带有附件的电子邮件。 附件可以是几种不同的文件类型(pdf,txt,doc,swf等)中的任何一种。
首先,脚本使用“ file_get_contents”获取文件。
后来,脚本在标头中回显:
Content-Type: <?php echo $the_content_type; ?>; name="<?php echo $the_file_name; ?>"
如何为$ the_content_type设置正确的值?
我正在使用此函数,其中包括几个后备功能,以补偿旧版本的PHP或简单的不良结果:
function getFileMimeType($file) {
if (function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $file);
finfo_close($finfo);
} else {
require_once 'upgradephp/ext/mime.php';
$type = mime_content_type($file);
}
if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {
$secondOpinion = exec('file -b --mime-type ' . escapeshellarg($file), $foo, $returnCode);
if ($returnCode === 0 && $secondOpinion) {
$type = $secondOpinion;
}
}
if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {
require_once 'upgradephp/ext/mime.php';
$exifImageType = exif_imagetype($file);
if ($exifImageType !== false) {
$type = image_type_to_mime_type($exifImageType);
}
}
return $type;
}
它尝试使用较新的PHP finfo
函数。 如果这些选项不可用,它将使用mime_content_type
替代方法,并包括Upgrade.php库中的直接替换项,以确保它存在。 如果没有返回任何有用的信息,它将尝试使用OS的file
命令。 仅在* NIX系统上可用的AFAIK,如果打算在Windows上使用它,则可能需要更改或取消该设置。 如果没有任何效果,它将尝试exif_imagetype
作为仅图像的后备。
我已经注意到,不同的服务器在支持mime类型功能方面差异很大,并且Upgrade.php mime_content_type
替换还远远不够完美。 有限的exif_imagetype
函数(无论是原始函数还是Upgrade.php替换函数)都可以正常运行。 如果您只关心图像,则可能只想使用最后一个。
在php中拥有它非常容易。
只需调用以下php函数mime_content_type
<?php
$filelink= 'uploads/some_file.pdf';
$the_content_type = "";
// check if the file exist before
if(is_file($file_link)) {
$the_content_type = mime_content_type($file_link);
}
// You can now use it here.
?>
使用finfo_file: http ://us2.php.net/manual/en/function.finfo-file.php
这是一个使用finfo_open的示例,该示例在PHP5和PECL中可用:
$mimepath='/usr/share/magic'; // may differ depending on your machine
// try /usr/share/file/magic if it doesn't work
$mime = finfo_open(FILEINFO_MIME,$mimepath);
if ($mime===FALSE) {
throw new Exception('Unable to open finfo');
}
$filetype = finfo_file($mime,$tmpFileName);
finfo_close($mime);
if ($filetype===FALSE) {
throw new Exception('Unable to recognise filetype');
}
另外,您可以使用已弃用的 mime_ content_类型函数:
$filetype=mime_content_type($tmpFileName);
或使用操作系统的内置功能:
ob_start();
system('/usr/bin/file -i -b ' . realpath($tmpFileName));
$type = ob_get_clean();
$parts = explode(';', $type);
$filetype=trim($parts[0]);
function getMimeType( $filename ) {
$realpath = realpath( $filename );
if ( $realpath
&& function_exists( 'finfo_file' )
&& function_exists( 'finfo_open' )
&& defined( 'FILEINFO_MIME_TYPE' )
) {
// Use the Fileinfo PECL extension (PHP 5.3+)
return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
}
if ( function_exists( 'mime_content_type' ) ) {
// Deprecated in PHP 5.3
return mime_content_type( $realpath );
}
return false;
}
这对我有用
我想我找到了很短的路。 使用以下方法获取图像大小:
$infFil=getimagesize($the_file_name);
和
Content-Type: <?php echo $infFil["mime"] ?>; name="<?php echo $the_file_name; ?>"
getimagesize
返回具有MIME密钥的关联数组
我用它并且有效
我已经尝试了大多数建议,但对我来说却都是失败的(我似乎介于任何有用的PHP版本之间。我最终获得了以下功能:
function getShellFileMimetype($file) {
$type = shell_exec('file -i -b '. escapeshellcmd( realpath($_SERVER['DOCUMENT_ROOT'].$file)) );
if( strpos($type, ";")!==false ){
$type = current(explode(";", $type));
}
return $type;
}
有函数头:
header('Content-Type: '.$the_content_type);
请注意,必须在任何输出之前调用此函数。 您可以在参考http://php.net/header中找到更多详细信息。
编辑:
操作,我误解了这个问题:自php 4.0起,有mime_content_type函数可检测文件的mimetype。
在php 5中已弃用,应由功能的文件信息集代替。
我真的建议使用“ CodeIgniter”之类的框架来查找电子邮件。 这是有关仅用18分钟即可发送“使用CodeIgniter发送电子邮件”的截屏视频。
http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-3/
尝试这个:
function ftype($f) {
curl_setopt_array(($c = @curl_init((!preg_match("/[a-z]+:\/{2}(?:www\.)?/i",$f) ? sprintf("%s://%s/%s", "http" , $_SERVER['HTTP_HOST'],$f) : $f))), array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 1));
return(preg_match("/Type:\s*(?<mime_type>[^\n]+)/i", @curl_exec($c), $m) && curl_getinfo($c, CURLINFO_HTTP_CODE) != 404) ? ($m["mime_type"]) : 0;
}
echo ftype("http://img2.orkut.com/images/medium/1283204135/604747203/ln.jpg"); // print image/jpeg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.