Index: file.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/file.inc,v retrieving revision 1.35 diff -u -r1.35 file.inc --- file.inc 24 Jan 2005 21:20:15 -0000 1.35 +++ file.inc 31 Jan 2005 17:23:02 -0000 @@ -132,14 +132,29 @@ return $source; } } - elseif ($_FILES["edit"]["name"][$source] && is_uploaded_file($_FILES["edit"]["tmp_name"][$source])) { + elseif ($_FILES['edit']['name'][$source] && is_uploaded_file($_FILES['edit']['tmp_name'][$source])) { $file = new StdClass(); - $file->filename = trim(basename($_FILES["edit"]["name"][$source]), '.'); - $file->filemime = $_FILES["edit"]["type"][$source]; - $file->filepath = $_FILES["edit"]["tmp_name"][$source]; - $file->error = $_FILES["edit"]["error"][$source]; - $file->filesize = $_FILES["edit"]["size"][$source]; + $file->filename = trim(basename($_FILES['edit']['name'][$source]), '.'); + $file->filepath = $_FILES['edit']['tmp_name'][$source]; + $file->error = $_FILES['edit']['error'][$source]; + $file->filesize = $_FILES['edit']['size'][$source]; $file->source = $source; + + // Poll known mimetype libraries to determine mimetype + // We don't use $_FILES["edit"]["type"] here because the + // user-agent may be presenting a false content type. + if (extension_loaded('fileinfo')) { + $res = finfo_open(FILEINFO_MIME); + $file->filemime = finfo_file($res, $file->filepath); + finfo_close($res); + } + else if (function_exists('mime_content_type')) { + $file->filemime = mime_content_type($file->filename); + } + else { + $file->filemime = file_mime_content_type($file->filename); + } + return $file; } else { @@ -523,6 +538,68 @@ } return $files; +} + +/** + * Determines the content type of a filename based on its extension. + * + * @param $filename + * The name of the file. + * + * @return + * A string containing the mimetype of the file. If the type is + * unknown, the result will be "application/octet-stream". + */ +function file_mime_content_type($filename) { + preg_match('/.*\.(.*)$/', $filename, $matches); + $extension = strtolower($matches[1]); + + $mimetypes = array( + 'jpg' => 'image/jpeg', + 'png' => 'image/png', + 'gif' => 'image/gif', + 'pdf' => 'application/pdf', + 'htm' => 'text/html', + 'html' => 'text/html', + 'css' => 'text/css', + 'mpg' => 'video/mpeg', + 'mpeg' => 'video/mpeg', + 'wma' => 'audio/x-ms-wma', + 'wmv' => 'video/x-ms-wmv', + 'mov' => 'video/quicktime', + 'avi' => 'video/x-msvideo', + 'mp3' => 'audio/mpeg3', + 'zip' => 'application/zip', + 'rar' => 'application/x-rar-compressed', + 'tar' => 'application/x-tar', + 'tgz' => 'application/x-compressed', + 'gz' => 'application/x-gzip', + 'z' => 'application/x-compress', + 'bz' => 'application/x-bz2-compressed', + 'bz2' => 'application/x-bz2-compressed', + 'sit' => 'application/x-stuffit', + 'sitx' => 'application/x-stuffit', + 'sea' => 'application/x-sea', + 'xml' => 'text/xml', + 'xsl' => 'text/xml', + 'svg' => 'image/svg+xml', + 'svgz' => 'image/svg+xml', + 'js' => 'text/javascript', + 'txt' => 'text/plain', + 'csv' => 'text/plain', + 'php' => 'text/plain', + 'inc' => 'text/plain', + 'patch' => 'text/plain', + 'diff' => 'text/plain', + 'module' => 'text/plain' + ); + + if (array_key_exists($extension, $mimetypes)) { + return $mimetypes[$extension]; + } + else { + return 'application/octet-stream'; + } } ?>