Is there a quick and dirty mapping of MIME types to extensions in PHP that I can make use of?
Answers
If your using Apache .htaccess can be used to map mime types.
moreinfo Here is IANA's List of MIME TYPES
I know this works for zip files, but I'm not too sure about xlsx files. It's worth a try:
To list the files in a zip archive:
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
for ($i=0; $i<$zip->numFiles; $i++) {
print_r($zip->statIndex($i));
}
$zip->close();
} else {
echo 'failed, code:' . $res;
}
This will print all the files like this:
Array
(
[name] => file.png
[index] => 2
[crc] => -485783131
[size] => 1486337
[mtime] => 1311209860
[comp_size] => 1484832
[comp_method] => 8
)
As you can see here, it gives the size
and the comp_size
for each archive. If it is an archive bomb, the ratio between these two numbers will be astronomical. You could simply put a limit of however many megabytes you want the maximum decompressed file size and if it exceeds that amount, skip that file and give an error message back to the user, else proceed with your extraction. See the manual for more information.
CodeProject has some classes you can download.
First get the FileAssociationInfo
, and from that get the ProgramAssociationInfo
. The pai
object can give you the icon.
FileAssociationInfo fai = new FileAssociationInfo(".bob");
ProgramAssociationInfo pai = new ProgramAssociationInfo(fai.ProgID);
ProgramIcon icon = pai.DefaultIcon;
This technique works well for me:
if (-not ([System.Management.Automation.PSTypeName]'MyClass').Type)
{
Add-Type -TypeDefinition 'public class MyClass { }'
}
- The type name can be enclosed in quotes 'MyClass', square brackets [MyClass], or both '[MyClass]' (v3+ only).
- The type name lookup is not case-sensitive.
- You must use the full name of the type, unless it is part of the System namespace (e.g. [System.DateTime] can be looked up via 'DateTime', but [System.Reflection.Assembly] cannot be looked up via 'Assembly').
- I've only tested this in Win8.1; PowerShell v2, v3, v4.
Internally, the PSTypeName class calls the LanguagePrimitives.ConvertStringToType() method which handles the heavy lifting. It caches the lookup string when successful, so additional lookups are faster.
I have not confirmed whether or not any exceptions are thrown internally as mentioned by x0n and Justin D.
Not built-in, but it's not terribly hard to roll your own: