vimalmichael
vimalmichael

Reputation: 25

Attaching files on a php form

I have included a code in my php form for attaching files, please find the code below. In my code I have mentioned to only to accept *.doc, *.docx and *.pdf but its accepting all the extensions

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." ;
                            }
                        }
                    }

Upvotes: 1

Views: 87

Answers (3)

Cristofor
Cristofor

Reputation: 2097

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;
}

Upvotes: 1

W Kristianto
W Kristianto

Reputation: 9303

You can use in_array() to check extension.

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);

Upvotes: 0

Coder
Coder

Reputation: 23

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) 

You can use something like this change image into word format.Or you can store the extensions in array and can check condition for efficent coding.I just gave you an idea NOT EXACT ANSWER

Upvotes: 0

Related Questions