Search code examples
phppdfmime

How to check whether the upload file is PDF in PHP


For an upload file type checking , I have implemented:

$_FILES["file"]["type"][$i] == 'application/pdf'

however, this checking will not work on the case I changed the extension name.

So , after some research, I have tried

$finfo = new finfo();
$fileMimeType = $finfo->file($_FILES["file"]["name"][$i] );

OR:

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileMimeType = finfo_file($finfo,$_FILES["file"]["name"][$i])

however, $fileMimeType echo nothing.

How to fix the problem? thanks


Solution

  • I guess the problem is using:

    $_FILES["my_file"]["name"]
    

    as it only contains the name of the uploaded file. If you want to check the file before moving it using move_uploaded_file you can refer to the temp file using:

    $_FILES["my_file"]["tmp_name"]