Advertisement
Guest User

convert mime to extension

a guest
Sep 13th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.20 KB | None | 0 0
  1. <?php
  2. dl('fileinfo');
  3. $unknown = [];
  4. foreach (new DirectoryIterator('.') as $file) {
  5.     if ($file->isFile()) {
  6.         $mime = mime_content_type($file->getPathname());
  7.         $validExt = mime2ext($mime);
  8.         if ($validExt) {
  9.             if (strcasecmp($validExt, $file->getExtension()) != 0) {
  10.                 echo $file.' - will rename to '.$validExt.PHP_EOL;
  11.                 $newName = $file->getBasename('.'.$file->getExtension()).'.'.$validExt;
  12.                 rename($file, $newName);
  13.             }
  14.         } else {
  15.             echo $file.' - unknown MIME'.PHP_EOL;
  16.             $unknown[] = $file->getBasename();
  17.             $newName = $file->getBasename('.'.$file->getExtension()).'.dat';
  18.             rename($file, $newName);
  19.         }
  20.     }
  21. }
  22.  
  23. echo PHP_EOL;
  24. echo 'Unknown files:'.PHP_EOL;
  25. foreach ($unknown as $u) echo $u.PHP_EOL;
  26.  
  27. // from: https://gist.github.com/alexcorvi/df8faecb59e86bee93411f6a7967df2c
  28. // changed 'jpeg' to 'jpg'
  29. function mime2ext($mime) {
  30.         static $mime_map = [
  31.             'video/3gpp2'                                                               => '3g2',
  32.             'video/3gp'                                                                 => '3gp',
  33.             'video/3gpp'                                                                => '3gp',
  34.             'application/x-compressed'                                                  => '7zip',
  35.             'audio/x-acc'                                                               => 'aac',
  36.             'audio/ac3'                                                                 => 'ac3',
  37.             'application/postscript'                                                    => 'ai',
  38.             'audio/x-aiff'                                                              => 'aif',
  39.             'audio/aiff'                                                                => 'aif',
  40.             'audio/x-au'                                                                => 'au',
  41.             'video/x-msvideo'                                                           => 'avi',
  42.             'video/msvideo'                                                             => 'avi',
  43.             'video/avi'                                                                 => 'avi',
  44.             'application/x-troff-msvideo'                                               => 'avi',
  45.             'application/macbinary'                                                     => 'bin',
  46.             'application/mac-binary'                                                    => 'bin',
  47.             'application/x-binary'                                                      => 'bin',
  48.             'application/x-macbinary'                                                   => 'bin',
  49.             'image/bmp'                                                                 => 'bmp',
  50.             'image/x-bmp'                                                               => 'bmp',
  51.             'image/x-bitmap'                                                            => 'bmp',
  52.             'image/x-xbitmap'                                                           => 'bmp',
  53.             'image/x-win-bitmap'                                                        => 'bmp',
  54.             'image/x-windows-bmp'                                                       => 'bmp',
  55.             'image/ms-bmp'                                                              => 'bmp',
  56.             'image/x-ms-bmp'                                                            => 'bmp',
  57.             'application/bmp'                                                           => 'bmp',
  58.             'application/x-bmp'                                                         => 'bmp',
  59.             'application/x-win-bitmap'                                                  => 'bmp',
  60.             'application/cdr'                                                           => 'cdr',
  61.             'application/coreldraw'                                                     => 'cdr',
  62.             'application/x-cdr'                                                         => 'cdr',
  63.             'application/x-coreldraw'                                                   => 'cdr',
  64.             'image/cdr'                                                                 => 'cdr',
  65.             'image/x-cdr'                                                               => 'cdr',
  66.             'zz-application/zz-winassoc-cdr'                                            => 'cdr',
  67.             'application/mac-compactpro'                                                => 'cpt',
  68.             'application/pkix-crl'                                                      => 'crl',
  69.             'application/pkcs-crl'                                                      => 'crl',
  70.             'application/x-x509-ca-cert'                                                => 'crt',
  71.             'application/pkix-cert'                                                     => 'crt',
  72.             'text/css'                                                                  => 'css',
  73.             'text/x-comma-separated-values'                                             => 'csv',
  74.             'text/comma-separated-values'                                               => 'csv',
  75.             'application/vnd.msexcel'                                                   => 'csv',
  76.             'application/x-director'                                                    => 'dcr',
  77.             'application/vnd.openxmlformats-officedocument.wordprocessingml.document'   => 'docx',
  78.             'application/x-dvi'                                                         => 'dvi',
  79.             'message/rfc822'                                                            => 'eml',
  80.             'application/x-msdownload'                                                  => 'exe',
  81.             'video/x-f4v'                                                               => 'f4v',
  82.             'audio/x-flac'                                                              => 'flac',
  83.             'video/x-flv'                                                               => 'flv',
  84.             'image/gif'                                                                 => 'gif',
  85.             'application/gpg-keys'                                                      => 'gpg',
  86.             'application/x-gtar'                                                        => 'gtar',
  87.             'application/x-gzip'                                                        => 'gzip',
  88.             'application/mac-binhex40'                                                  => 'hqx',
  89.             'application/mac-binhex'                                                    => 'hqx',
  90.             'application/x-binhex40'                                                    => 'hqx',
  91.             'application/x-mac-binhex40'                                                => 'hqx',
  92.             'text/html'                                                                 => 'html',
  93.             'image/x-icon'                                                              => 'ico',
  94.             'image/x-ico'                                                               => 'ico',
  95.             'image/vnd.microsoft.icon'                                                  => 'ico',
  96.             'text/calendar'                                                             => 'ics',
  97.             'application/java-archive'                                                  => 'jar',
  98.             'application/x-java-application'                                            => 'jar',
  99.             'application/x-jar'                                                         => 'jar',
  100.             'image/jp2'                                                                 => 'jp2',
  101.             'video/mj2'                                                                 => 'jp2',
  102.             'image/jpx'                                                                 => 'jp2',
  103.             'image/jpm'                                                                 => 'jp2',
  104.             'image/jpeg'                                                                => 'jpg',
  105.             'image/pjpeg'                                                               => 'jpg',
  106.             'application/x-javascript'                                                  => 'js',
  107.             'application/json'                                                          => 'json',
  108.             'text/json'                                                                 => 'json',
  109.             'application/vnd.google-earth.kml+xml'                                      => 'kml',
  110.             'application/vnd.google-earth.kmz'                                          => 'kmz',
  111.             'text/x-log'                                                                => 'log',
  112.             'audio/x-m4a'                                                               => 'm4a',
  113.             'application/vnd.mpegurl'                                                   => 'm4u',
  114.             'audio/midi'                                                                => 'mid',
  115.             'application/vnd.mif'                                                       => 'mif',
  116.             'video/quicktime'                                                           => 'mov',
  117.             'video/x-sgi-movie'                                                         => 'movie',
  118.             'audio/mpeg'                                                                => 'mp3',
  119.             'audio/mpg'                                                                 => 'mp3',
  120.             'audio/mpeg3'                                                               => 'mp3',
  121.             'audio/mp3'                                                                 => 'mp3',
  122.             'video/mp4'                                                                 => 'mp4',
  123.             'video/mpeg'                                                                => 'mpeg',
  124.             'application/oda'                                                           => 'oda',
  125.             'audio/ogg'                                                                 => 'ogg',
  126.             'video/ogg'                                                                 => 'ogg',
  127.             'application/ogg'                                                           => 'ogg',
  128.             'application/x-pkcs10'                                                      => 'p10',
  129.             'application/pkcs10'                                                        => 'p10',
  130.             'application/x-pkcs12'                                                      => 'p12',
  131.             'application/x-pkcs7-signature'                                             => 'p7a',
  132.             'application/pkcs7-mime'                                                    => 'p7c',
  133.             'application/x-pkcs7-mime'                                                  => 'p7c',
  134.             'application/x-pkcs7-certreqresp'                                           => 'p7r',
  135.             'application/pkcs7-signature'                                               => 'p7s',
  136.             'application/pdf'                                                           => 'pdf',
  137.             'application/octet-stream'                                                  => 'pdf',
  138.             'application/x-x509-user-cert'                                              => 'pem',
  139.             'application/x-pem-file'                                                    => 'pem',
  140.             'application/pgp'                                                           => 'pgp',
  141.             'application/x-httpd-php'                                                   => 'php',
  142.             'application/php'                                                           => 'php',
  143.             'application/x-php'                                                         => 'php',
  144.             'text/php'                                                                  => 'php',
  145.             'text/x-php'                                                                => 'php',
  146.             'application/x-httpd-php-source'                                            => 'php',
  147.             'image/png'                                                                 => 'png',
  148.             'image/x-png'                                                               => 'png',
  149.             'application/powerpoint'                                                    => 'ppt',
  150.             'application/vnd.ms-powerpoint'                                             => 'ppt',
  151.             'application/vnd.ms-office'                                                 => 'ppt',
  152.             'application/msword'                                                        => 'doc',
  153.             'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
  154.             'application/x-photoshop'                                                   => 'psd',
  155.             'image/vnd.adobe.photoshop'                                                 => 'psd',
  156.             'audio/x-realaudio'                                                         => 'ra',
  157.             'audio/x-pn-realaudio'                                                      => 'ram',
  158.             'application/x-rar'                                                         => 'rar',
  159.             'application/rar'                                                           => 'rar',
  160.             'application/x-rar-compressed'                                              => 'rar',
  161.             'audio/x-pn-realaudio-plugin'                                               => 'rpm',
  162.             'application/x-pkcs7'                                                       => 'rsa',
  163.             'text/rtf'                                                                  => 'rtf',
  164.             'text/richtext'                                                             => 'rtx',
  165.             'video/vnd.rn-realvideo'                                                    => 'rv',
  166.             'application/x-stuffit'                                                     => 'sit',
  167.             'application/smil'                                                          => 'smil',
  168.             'text/srt'                                                                  => 'srt',
  169.             'image/svg+xml'                                                             => 'svg',
  170.             'application/x-shockwave-flash'                                             => 'swf',
  171.             'application/x-tar'                                                         => 'tar',
  172.             'application/x-gzip-compressed'                                             => 'tgz',
  173.             'image/tiff'                                                                => 'tiff',
  174.             'text/plain'                                                                => 'txt',
  175.             'text/x-vcard'                                                              => 'vcf',
  176.             'application/videolan'                                                      => 'vlc',
  177.             'text/vtt'                                                                  => 'vtt',
  178.             'audio/x-wav'                                                               => 'wav',
  179.             'audio/wave'                                                                => 'wav',
  180.             'audio/wav'                                                                 => 'wav',
  181.             'application/wbxml'                                                         => 'wbxml',
  182.             'video/webm'                                                                => 'webm',
  183.             'audio/x-ms-wma'                                                            => 'wma',
  184.             'application/wmlc'                                                          => 'wmlc',
  185.             'video/x-ms-wmv'                                                            => 'wmv',
  186.             'video/x-ms-asf'                                                            => 'wmv',
  187.             'application/xhtml+xml'                                                     => 'xhtml',
  188.             'application/excel'                                                         => 'xl',
  189.             'application/msexcel'                                                       => 'xls',
  190.             'application/x-msexcel'                                                     => 'xls',
  191.             'application/x-ms-excel'                                                    => 'xls',
  192.             'application/x-excel'                                                       => 'xls',
  193.             'application/x-dos_ms_excel'                                                => 'xls',
  194.             'application/xls'                                                           => 'xls',
  195.             'application/x-xls'                                                         => 'xls',
  196.             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'         => 'xlsx',
  197.             'application/vnd.ms-excel'                                                  => 'xlsx',
  198.             'application/xml'                                                           => 'xml',
  199.             'text/xml'                                                                  => 'xml',
  200.             'text/xsl'                                                                  => 'xsl',
  201.             'application/xspf+xml'                                                      => 'xspf',
  202.             'application/x-compress'                                                    => 'z',
  203.             'application/x-zip'                                                         => 'zip',
  204.             'application/zip'                                                           => 'zip',
  205.             'application/x-zip-compressed'                                              => 'zip',
  206.             'application/s-compressed'                                                  => 'zip',
  207.             'multipart/x-zip'                                                           => 'zip',
  208.             'text/x-scriptzsh'                                                          => 'zsh',
  209.         ];
  210.  
  211.         return isset($mime_map[$mime]) === true ? $mime_map[$mime] : false;
  212.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement