1

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." ;
                            }
                        }
                    }
vimalmichael
  • 25
  • 1
  • 5
  • possible duplicate of [How can I only allow certain filetypes on upload in php?](http://stackoverflow.com/questions/2486329/how-can-i-only-allow-certain-filetypes-on-upload-in-php) – Shmwel Jun 11 '14 at 05:46

3 Answers3

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;
}
Cristofor
  • 2,077
  • 2
  • 15
  • 23
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) 

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

Coder
  • 23
  • 1
  • 9
0

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);
Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68