Question

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

Was it helpful?

Solution 2

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"]

OTHER TIPS

Read the first 4 bytes of the file and check that they match %PDF.

$filename = "pdffile";
$handle = fopen($filename, "r");
$header = fread($handle, 4);
fclose($handle);

Check $header against %PDF

if you read the file using fread you need to have a dictionary of all the file header type definitions. If you want to use the file shell command

$out = exec("file 'R-intro.pdf' | cut -d: -f2 | cut -d, -f1");
if (trim($out) == "PDF document") {
   echo "1";
}

To further expand on how to replace the constant file name with a uploaded file refer below.

$out = exec("file '" . $_FILES['file']['tmp_name'] . "' | cut -d: -f2 | cut -d, -f1");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top