我正在使用 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 发送带有附件的电子邮件。附件可以是多种不同文件类型(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;
}
它尝试使用较新的 PHPfinfo
函数。如果这些不可用,它会使用mime_content_type
替代方法并包括Upgrade.php库中的直接替换以确保它存在。如果那些没有返回任何有用的东西,它会尝试操作系统的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.
?>
函数 mime_content_type() 的 PHP 文档 希望对某人有所帮助
使用 finfo_file:http ://us2.php.net/manual/en/function.finfo-file.php
这是一个使用PHP5 和 PECL 中可用的finfo_open的示例:
$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_type函数:
$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;
}
尝试这个:
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
我真的建议使用像“CodeIgniter”这样的框架来处理电子邮件。这是一个关于“在 18 分钟内使用 CodeIgniter 发送电子邮件”的截屏视频。
http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-3/