How can PHP be used to identify different file types without using a case for each possible extension?
To identify different file types without using a case for each possible extension, you can utilize PHP's `finfo` extension. This extension allows you to determine the MIME type of a file based on its contents rather than relying solely on the file extension. By using this method, you can accurately identify file types regardless of their extension.
$file = 'example.txt';
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $file);
finfo_close($finfo);
echo "The MIME type of $file is: $mime_type";
Related questions
-
syntax errors PHP code string concatenation
-
PHP image URLs failed to open stream
-
SQL injection prepared statements PDO