phpzipmime-typesrar

.rar, .zip files MIME Type


I'm developing a simple php upload script, and users can upload only ZIP and RAR files.

What MIME types I should use to check $_FILES[x][type]? (a complete list please)


Solution

  • The answers from freedompeace, Kiyarash and Sam Vloeberghs:

    .rar    application/vnd.rar, application/x-rar-compressed, application/octet-stream
    .zip    application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip
    

    I would do a check on the file name too. Here is how you could check if the file is a RAR or ZIP file. I tested it by creating a quick command line application.

    <?php
    
    if (isRarOrZip($argv[1])) {
        echo 'It is probably a RAR or ZIP file.';
    } else {
        echo 'It is probably not a RAR or ZIP file.';
    }
    
    function isRarOrZip($file) {
        // get the first 7 bytes
        $bytes = file_get_contents($file, FALSE, NULL, 0, 7);
        $ext = strtolower(substr($file, - 4));
    
        // RAR magic number: Rar!\x1A\x07\x00
        // http://en.wikipedia.org/wiki/RAR
        if ($ext == '.rar' and bin2hex($bytes) == '526172211a0700') {
            return TRUE;
        }
    
        // ZIP magic number: none, though PK\003\004, PK\005\006 (empty archive), 
        // or PK\007\008 (spanned archive) are common.
        // http://en.wikipedia.org/wiki/ZIP_(file_format)
        if ($ext == '.zip' and substr($bytes, 0, 2) == 'PK') {
            return TRUE;
        }
    
        return FALSE;
    }
    

    Notice that it still won't be 100% certain, but it is probably good enough.

    $ rar.exe l somefile.zip
    somefile.zip is not RAR archive
    

    But even WinRAR detects non RAR files as SFX archives:

    $ rar.exe l somefile.srr
    SFX Volume somefile.srr