在php表单上附加文件

时间:2014-06-11 05:41:41

标签: php

我在php表单中包含了一个用于附加文件的代码,请在下面找到代码。在我的代码中,我提到只接受* .doc,* .docx和* .pdf,但它接受所有扩展

function checkType() {
                    if(!empty($_FILES['fileatt']['type'])){
                        if(($_FILES['fileatt']['type'] != "application/msword")||($_FILES['fileatt']['type'] != "application/vnd.openxmlformats-officedocument.wordprocessingml.document")||($_FILES['fileatt']['type'] != "application/pdf")) {
                            echo "Sorry, current format is <b> (".$_FILES['fileatt']['type'].")</b>, only *.doc, *.docx and *.pdf are allowed." ;
                            }
                        }
                    }

3 个答案:

答案 0 :(得分:1)

function checkType() {
    if(!empty($_FILES['fileatt']['type'])){
        $allowed =  array('doc','docx' ,'pdf');
        $filename = $_FILES['fileatt']['name'];
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!in_array($ext,$allowed) ) {
            echo "Sorry, current format is <b> (".$_FILES['fileatt']['type'].")</b>, only *.doc, *.docx and *.pdf are allowed." ;
            return false;
       }
    }
    return true;
}

答案 1 :(得分:0)

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000) 

你可以使用像这样的变化图像到word格式。或者你可以将扩展存储在数组中,并且可以检查有效编码的条件。我只是给你一个想法并不是完全答案

答案 2 :(得分:0)

您可以使用in_array()检查扩展程序。

function checkType($data, $ext)
{
    $filename   = $data['name'];
    $pathinfo   = pathinfo($filename, PATHINFO_EXTENSION);

    // Checking
    if(in_array($pathinfo, $ext))
    {
        // TRUE
        return TRUE;
    }

    // FALSE
    return FALSE;
}

$checkType = checkType($_FILES['video_file'], array('doc', 'docx', 'pdf');

var_dump($checkType);
相关问题