Checking the type of mime pdf files when loading in PHP

I am working on a project that requires downloading files. I want to make sure that it is protected and only PDF files are uploaded. I am already checking the file extension, but I want to make sure that the file is really a PDF file.

What is the best way to check mime type in php? I am using PHP 5.2.5 and cannot get fileinfo or mime_content_type () to work.

for fileinfo, I keep getting the following:

Warning: finfo_open() [function.finfo-open]: Failed to load magic database
at '(null)'. in [snipped filename]  on line 35
+3
source share
4 answers

Mime types are not reliable for checking file types. The client browser may report an error.

Check the magic number . PDF files begin with "% PDF" (25 50 44 46).

+5

, MIME , , , , .

, mime :

$Type = $_FILES['someFile']['type'];

, php-, PDF , FPDF (http://www.fpdf.org/)

, :)

+1

, , MAGIC , /usr/share/misc/magic. MAGIC , finfo

$finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic");

$finfo = finfo_open(FILEINFO_MIME, "/usr/share/misc/magic"); 
+1

MIME- $_FILES. mime "pdf", PDF.

$contentType = $_FILES['myFile']['type'];
if(isValidPDF($contentType)) {
    die('It is a PDF');
} else {
    die('It is not a PDF');
}

function isValidPDF($mime) {
    return strpos($mime, 'pdf') !== false;
}
-2

Source: https://habr.com/ru/post/1756933/


All Articles