我正在使用PHP发送带附件的电子邮件.附件可以是几种不同文件类型中的任何一种(pdf,txt,doc,swf等).
首先,脚本使用"file_get_contents"获取文件.
之后,脚本在标题中回响:
Content-Type: <?php echo $the_content_type; ?>; name="<?php echo $the_file_name; ?>"
Run Code Online (Sandbox Code Playgroud)
如何为$ the_content_type设置正确的值?
我注意到get_mime()现在已经折旧,那么创建以下函数的替代方法是什么?此功能通过我使用的cms使用,因此需要一个可靠的替代方案.
// Mime Type Checker
function get_mime ($filename,$mode=0) {
// mode 0 = full check
// mode 1 = extension check only
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => …Run Code Online (Sandbox Code Playgroud) 我有一个输入,您可以在其中上传图像,唯一允许的图像类型是:
png, jpg, jpeg
Run Code Online (Sandbox Code Playgroud)
在将图像插入数据库之前,它会检查图片是否为 png、jpg、jpeg。但现在出于安全原因,我需要在第一次检查之前或之后检查 MIME 类型。
我该怎么做呢?这是我的代码:
<?php
$iAmountOfFiles = count($_FILES['Filename']['name']);
while($iAmountOfFiles >= 1) {
$iAmountOfFiles--;
$aFileProperties = pathinfo($_FILES['Filename']['name'][$iAmountOfFiles]);
if(!in_array(strtolower($aFileProperties["extension"]), $aExtensionWhitelist)) {
echo "Bestands type niet toegestaan";
// exit;
continue;
}
$sTarget = ROOT.BACKEND."/pages/bezienswaardigheden-toevoegen/uploads/";
$sUniqueFileNameHash = hash('adler32', time().rand());
$Filename = basename($sUniqueFileNameHash."-".$_FILES['Filename']['name'][$iAmountOfFiles]);
$Filename = basename($aFileProperties["filename"]."-".$sUniqueFileNameHash.".".strtolower($aFileProperties["extension"]));
// Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'][$iAmountOfFiles], $sTarget.$Filename)) {
// here needs to come the mime check
Run Code Online (Sandbox Code Playgroud)