I'm using PHP to send an email with an attachment. The attachment could be any of several different file types (pdf, txt, doc, swf, etc).
我正在使用PHP发送带附件的电子邮件。附件可以是几种不同文件类型中的任何一种(pdf,txt,doc,swf等)。
First, the script gets the file using "file_get_contents".
首先,脚本使用“file_get_contents”获取文件。
Later, the script echoes in the header:
之后,脚本在标题中回响:
Content-Type: <?php echo $the_content_type; ?>; name="<?php echo $the_file_name; ?>"
How to I set the correct value for $the_content_type?
如何为$ the_content_type设置正确的值?
10 个解决方案
#1
25
I am using this function, which includes several fallbacks to compensate for older versions of PHP or simply bad results:
我正在使用此功能,其中包括几个回退以补偿旧版本的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;
}
It tries to use the newer PHP finfo
functions. If those aren't available, it uses the mime_content_type
alternative and includes the drop-in replacement from the Upgrade.php library to make sure this exists. If those didn't return anything useful, it'll try the OS' file
command. AFAIK that's only available on *NIX systems, you may want to change that or get rid of it if you plan to use this on Windows. If nothing worked, it tries exif_imagetype
as fallback for images only.
它尝试使用较新的PHP finfo函数。如果这些不可用,它将使用mime_content_type替代方法,并包含Upgrade.php库中的替代项,以确保它存在。如果那些没有返回任何有用的东西,它将尝试OS'文件命令。 AFAIK只能在* NIX系统上使用,如果您计划在Windows上使用它,您可能想要更改或删除它。如果没有任何效果,它会尝试将exif_imagetype作为图像的后备。
I have come to notice that different servers vary widely in their support for the mime type functions, and that the Upgrade.php mime_content_type
replacement is far from perfect. The limited exif_imagetype
functions, both the original and the Upgrade.php replacement, are working pretty reliably though. If you're only concerned about images, you may only want to use this last one.
我注意到不同的服务器在支持mime类型函数方面差别很大,而且upgrade.php mime_content_type的替换远非完美。有限的exif_imagetype函数,无论是原始函数还是Upgrade.php替换版,都可以非常可靠地工作。如果你只关心图像,你可能只想使用最后一个。
#2
6
With finfo_file: http://us2.php.net/manual/en/function.finfo-file.php
使用finfo_file:http://us2.php.net/manual/en/function.finfo-file.php
#3
5
It very easy to have it in php.
它很容易在PHP中使用它。
Simply call the following php function mime_content_type
只需调用以下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.
?>
PHP documentation of the function mime_content_type() Hope it helps someone
函数的PHP文档mime_content_type()希望它可以帮助某人
#4
4
Here's an example using finfo_open which is available in PHP5 and PECL:
这是一个使用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');
}
Alternatively, you can use the deprecated mime_ content_ type function:
或者,您可以使用已弃用的mime_ content_ type函数:
$filetype=mime_content_type($tmpFileName);
or use the OS's in built functions:
或者在内置函数中使用OS:
ob_start();
system('/usr/bin/file -i -b ' . realpath($tmpFileName));
$type = ob_get_clean();
$parts = explode(';', $type);
$filetype=trim($parts[0]);
#5
2
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;
}
This worked for me
这对我有用
Why is mime_content_type() deprecated in PHP?
为什么在PHP中不推荐使用mime_content_type()?
#6
0
I guess that i found a short way. Get the image size using:
我想我找到了一个简短的方法。使用以下方法获取图像大小
$infFil=getimagesize($the_file_name);
$ infFil =和getimagesize($ the_file_name);
and
和
Content-Type: <?php echo $infFil["mime"] ?>; name="<?php echo $the_file_name; ?>"
The getimagesize
returns an associative array which have a MIME key
getimagesize返回一个具有MIME密钥的关联数组
I used it and it works
我用它,它的工作原理
#7
0
I've tried most of the suggestions, but they all fail for me (I'm inbetween any usefull version of PHP apparantly. I ended up with the following function:
我已经尝试了大部分的建议,但它们都失败了(我正处于任何有用的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;
}
#8
-2
There is the function header:
有功能标题:
header('Content-Type: '.$the_content_type);
Note that this function has to be called before any output. You can find further details in the reference http://php.net/header
请注意,必须在任何输出之前调用此函数。您可以在参考http://php.net/header中找到更多详细信息
Edit:
编辑:
Ops, I've misunderstood the question: Since php 4.0 there is the function mime_content_type to detect the mimetype of a file.
Ops,我误解了这个问题:自php 4.0起,有一个函数mime_content_type来检测文件的mimetype。
In php 5 is deprecated, should be replaced by the file info set of functions.
在PHP 5中不推荐使用,应该由文件信息集替换。
#9
-3
I really recommend using a Framework like "CodeIgniter" for seinding Emails. Here is a Screencast about "Sending Emails with CodeIgniter" in only 18 Minutes.
我真的建议使用类似“CodeIgniter”的框架来搜索电子邮件。这是一个关于“使用CodeIgniter发送电子邮件”的截屏视频,只需18分钟。
http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-3/
http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-3/
#10
-3
try this:
尝试这个:
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
#1
25
I am using this function, which includes several fallbacks to compensate for older versions of PHP or simply bad results:
我正在使用此功能,其中包括几个回退以补偿旧版本的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;
}
It tries to use the newer PHP finfo
functions. If those aren't available, it uses the mime_content_type
alternative and includes the drop-in replacement from the Upgrade.php library to make sure this exists. If those didn't return anything useful, it'll try the OS' file
command. AFAIK that's only available on *NIX systems, you may want to change that or get rid of it if you plan to use this on Windows. If nothing worked, it tries exif_imagetype
as fallback for images only.
它尝试使用较新的PHP finfo函数。如果这些不可用,它将使用mime_content_type替代方法,并包含Upgrade.php库中的替代项,以确保它存在。如果那些没有返回任何有用的东西,它将尝试OS'文件命令。 AFAIK只能在* NIX系统上使用,如果您计划在Windows上使用它,您可能想要更改或删除它。如果没有任何效果,它会尝试将exif_imagetype作为图像的后备。
I have come to notice that different servers vary widely in their support for the mime type functions, and that the Upgrade.php mime_content_type
replacement is far from perfect. The limited exif_imagetype
functions, both the original and the Upgrade.php replacement, are working pretty reliably though. If you're only concerned about images, you may only want to use this last one.
我注意到不同的服务器在支持mime类型函数方面差别很大,而且upgrade.php mime_content_type的替换远非完美。有限的exif_imagetype函数,无论是原始函数还是Upgrade.php替换版,都可以非常可靠地工作。如果你只关心图像,你可能只想使用最后一个。
#2
6
With finfo_file: http://us2.php.net/manual/en/function.finfo-file.php
使用finfo_file:http://us2.php.net/manual/en/function.finfo-file.php
#3
5
It very easy to have it in php.
它很容易在PHP中使用它。
Simply call the following php function mime_content_type
只需调用以下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.
?>
PHP documentation of the function mime_content_type() Hope it helps someone
函数的PHP文档mime_content_type()希望它可以帮助某人
#4
4
Here's an example using finfo_open which is available in PHP5 and PECL:
这是一个使用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');
}
Alternatively, you can use the deprecated mime_ content_ type function:
或者,您可以使用已弃用的mime_ content_ type函数:
$filetype=mime_content_type($tmpFileName);
or use the OS's in built functions:
或者在内置函数中使用OS:
ob_start();
system('/usr/bin/file -i -b ' . realpath($tmpFileName));
$type = ob_get_clean();
$parts = explode(';', $type);
$filetype=trim($parts[0]);
#5
2
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;
}
This worked for me
这对我有用
Why is mime_content_type() deprecated in PHP?
为什么在PHP中不推荐使用mime_content_type()?
#6
0
I guess that i found a short way. Get the image size using:
我想我找到了一个简短的方法。使用以下方法获取图像大小
$infFil=getimagesize($the_file_name);
$ infFil =和getimagesize($ the_file_name);
and
和
Content-Type: <?php echo $infFil["mime"] ?>; name="<?php echo $the_file_name; ?>"
The getimagesize
returns an associative array which have a MIME key
getimagesize返回一个具有MIME密钥的关联数组
I used it and it works
我用它,它的工作原理
#7
0
I've tried most of the suggestions, but they all fail for me (I'm inbetween any usefull version of PHP apparantly. I ended up with the following function:
我已经尝试了大部分的建议,但它们都失败了(我正处于任何有用的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;
}
#8
-2
There is the function header:
有功能标题:
header('Content-Type: '.$the_content_type);
Note that this function has to be called before any output. You can find further details in the reference http://php.net/header
请注意,必须在任何输出之前调用此函数。您可以在参考http://php.net/header中找到更多详细信息
Edit:
编辑:
Ops, I've misunderstood the question: Since php 4.0 there is the function mime_content_type to detect the mimetype of a file.
Ops,我误解了这个问题:自php 4.0起,有一个函数mime_content_type来检测文件的mimetype。
In php 5 is deprecated, should be replaced by the file info set of functions.
在PHP 5中不推荐使用,应该由文件信息集替换。
#9
-3
I really recommend using a Framework like "CodeIgniter" for seinding Emails. Here is a Screencast about "Sending Emails with CodeIgniter" in only 18 Minutes.
我真的建议使用类似“CodeIgniter”的框架来搜索电子邮件。这是一个关于“使用CodeIgniter发送电子邮件”的截屏视频,只需18分钟。
http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-3/
http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-3/
#10
-3
try this:
尝试这个:
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