Determine file type in Ruby

There is a ruby binding to libmagic that does what you need. It is available as a gem named ruby-filemagic: gem install ruby-filemagic Require libmagic-dev. The documentation seems a little thin, but this should get you started: $ irb irb(main):001:0> require ‘filemagic’ => true irb(main):002:0> fm = FileMagic.new => #<FileMagic:0x7fd4afb0> irb(main):003:0> fm.file(‘foo.zip’) => “Zip archive … Read more

How to reliably detect file types? [duplicate]

File type detection tools: Mime Type Detection Utility DROID (Digital Record Object Identification) ftc – File Type Classifier JHOVE, JHOVE2 NLNZ Metadata Extraction Tool Apache Tika TrID, TrIDNet Oracle Outside In (commercial) Forensic Innovations File Investigator TOOLS (commercial) Apache Tika gives me the least amount of issues and is not platform specific unlike Java 7 … Read more

php file upload, how to restrict file upload type

The below just uses the mime types to validate a file, then checks the size of both. For a list of most mime types see here or google. function allowed_file(){ //Add the allowed mime-type files to an ‘allowed’ array $allowed = array(‘application/doc’, ‘application/pdf’, ‘another/type’); //Check uploaded file type is in the above array (therefore valid) … Read more

How to get file name from content-disposition

Here is how I used it sometime back. I’m assuming you are providing the attachment as a server response. I set the response header like this from my REST service response.setHeader(“Content-Disposition”, “attachment;filename=XYZ.csv”); function(response, status, xhr){ var filename = “”; var disposition = xhr.getResponseHeader(‘Content-Disposition’); if (disposition && disposition.indexOf(‘attachment’) !== -1) { var filenameRegex = /filename[^;=\n]*=(([‘”]).*?\2|[^;\n]*)/; var … Read more

What are the Google Apps MIME Types in Google Docs and Google Drive?

Google Docs: application/vnd.google-apps.document application/vnd.google-apps.kix Google Presentations: application/vnd.google-apps.presentation Google Spreadsheets: application/vnd.google-apps.spreadsheet Google Drawing: application/vnd.google-apps.drawing See here for more information. Here is a long list of Google Docs and Google Drive MIME types (it is not exhaustive): application/vnd.google-apps.audio application/vnd.google-apps.document application/vnd.google-apps.drawing application/vnd.google-apps.file application/vnd.google-apps.folder application/vnd.google-apps.form application/vnd.google-apps.fusiontable application/vnd.google-apps.kix application/vnd.google-apps.photo application/vnd.google-apps.presentation application/vnd.google-apps.script application/vnd.google-apps.sites application/vnd.google-apps.spreadsheet application/vnd.google-apps.unknown application/vnd.google-apps.video

Register file extensions / mime types in Linux

Use xdg-utils from freedesktop.org Portland. Register the icon for the MIME type: xdg-icon-resource install –context mimetypes –size 48 myicon-file-type.png x-application-mytype Create a configuration file (freedesktop Shared MIME documentation): <?xml version=”1.0″?> <mime-info xmlns=”http://www.freedesktop.org/standards/shared-mime-info”> <mime-type type=”application/x-mytype”> <comment>A witty comment</comment> <comment xml:lang=”it”>Uno Commento</comment> <glob pattern=”*.myapp”/> </mime-type> </mime-info> Install the configuration file: xdg-mime install mytype-mime.xml This gets your files … Read more

How to have jQuery restrict file types on upload?

You can get the value of a file field just the same as any other field. You can’t alter it, however. So to superficially check if a file has the right extension, you could do something like this: var ext = $(‘#my_file_field’).val().split(‘.’).pop().toLowerCase(); if($.inArray(ext, [‘gif’,’png’,’jpg’,’jpeg’]) == -1) { alert(‘invalid extension!’); }

Register a new file type in Android

I’m using this manifest to register (for example) a .stl file type with my application: <?xml version=”1.0″ encoding=”utf-8″?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”org.test.core” android:versionCode=”1″ android:versionName=”1.0″> <application android:icon=”@drawable/icon” android:label=”@string/app_name”> <activity android:name=”.Testy” android:label=”@string/app_name”> <intent-filter> <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> <activity android:name=”ThorActivity” android:label=”@string/app_name”> </activity> <activity android:name=”LokiActivity” android:label=”@string/app_name”> </activity> <activity android:name=”OdinActivity” android:label=”@string/app_name”> <intent-filter> <action android:name=”android.intent.action.VIEW” /> <category android:name=”android.intent.category.DEFAULT” … Read more