Fileinfo broken?


#1

Fileinfo is supposed to be bundled into php 5.3.0 and later. On Scott, php is 5.4.37 and the configuration line includes ‘–enable-fileinfo=shared’. Nonetheless, the fileinfo functions get “undefined function” errors.

I tried both the fileinfo version and the older mime_content_type:

[code] $finfo = finfo_open();
$mtype = finfo_file($finfo, $filename, FILEINFO_MIME);
OR
$mtype = mime_content_type($page);

		header("Content-Type: $mtype");[/code]

How do I get mime types??


#2

Did you ever get this working? What did you do?
I am having an issue with “fileinfo.so” giving me a fatal php error.


#3

File_info is now working.

My php is currently 5.6.10 and the phpinfo section for fileinfo says:

[quote] fileinfo support enabled
version 1.0.5
libmagic 517
[/quote]

I am getting mime info with the code below. (php_debug logs text to a file, but only if the requestor is me.) [php]/** return a mime type for $file
$file must be site-rooted
*/
function mimetype($file) {
global $sp;
$frfile = $sp->root.$file;

// try finfo (if it exists)
if (function_exists("finfo_open") 
		&& defined("FILEINFO_MIME")
		&& ($finfo=@finfo_open(FILEINFO_MIME)) != false) {
	$mt = @finfo_file($finfo, $frfile);
	if (is_string($mt) && ! empty($mt)) {
		php_debug("servepage: mimetype via file_info: $mt");
		return $mt;
	}
}

// try file -b -i
$filecmd = "/usr/bin/file -i -b $frfile";
$mt = @exec($filecmd);
if (is_string($mt) && !empty($mt)) {
	php_debug("servepage: mimetype via file cmd: $mt");
	return $mt;
}

// else if file is xml or text, send it appropriately
$filetype = @exec("/usr/bin/file $frfile");
php_debug("servepage: bin/file : $filetype");
if (is_string($filetype)) {
	if (stripos($filetype, "xml") !== FALSE) 
		return "appplication/xml; charset=UTF-8";
	if (stripos($filetype, "text") !== FALSE) 
		return "text/plain; charset=UTF-8";
}

// else send it as application/octet-stream
return "application/octet-stream; charset=binary";

}

////////////////////////////////////////

$file = $sp->thispage;
$frfile = $sp->root.$file;

php_debug("servepage: now serving $file");

if ( ! is_readable($frfile))
	pagefail($file);  // well duh, cannot serve an unreadable file

$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
switch ($ext) {
	case "bmp":  $mtype = "image/bmp; charset=UTF-8"; break;
	case "css":  $mtype = "text/css; charset=UTF-8"; break;
	case "doc":  $mtype = "application/msword; charset=UTF-8"; break;
	case "gif":  $mtype = "image/gif; charset=UTF-8"; break;
	case "html": $mtype = "text/html; charset=UTF-8"; break;
	case "htm":  $mtype = "text/html; charset=UTF-8"; break;
	case "ico":  $mtype = "image/x-icon; charset=UTF-8"; break;
	case "jar":  $mtype = "application/java-archive; charset=UTF-8"; break;
	case "jnlp": $mtype = "application/x-java-jnlp-file; charset=UTF-8"; break;
	case "jpg":  $mtype = "image/jpeg; charset=UTF-8"; break;
	case "js":   $mtype = "application/javascript; charset=UTF-8"; break;
	case "pdf":  $mtype = "application/pdf; charset=binary"; break;
	case "php":  $mtype = "text/plain; charset=UTF-8"; break;
	case "png":  $mtype = "image/png; charset=UTF-8"; break;
	case "ppt":  $mtype = "application/vnd.ms-powerpoint; charset=UTF-8"; break;
	case "txt":  $mtype = "text/plain; charset=UTF-8"; break;
	case "xls":  $mtype = "application/vnd.ms-excel; charset=UTF-8"; break;
	case "xml":  $mtype = "appplication/xml; charset=UTF-8"; break;
	
	// `file -i-b` gets these wrong *sometimes*
	// the //or values cause at least one version of firefox 
	//     to do download insteasd of display
	case "c":    $mtype = "text/plain; charset=UTF-8"; break;  // or text/x-c
	case "h":    $mtype = "text/plain; charset=UTF-8"; break;  // or text/x-c
	case "css":  $mtype = "text/css; charset=UTF-8"; break;
	case "csh":  $mtype = "text/plain; charset=UTF-8"; break;  // or text/x-shellscript
	case "js":   $mtype = "text/javascript; charset=UTF-8"; break;
	
	default: 
		// unknown file exension
		$mtype = mimetype($file);
		php_debug("servepage: mimetype() => $mtype");

... }[/php]