.rar, .zip files MIME Type

后端 未结 6 642
自闭症患者
自闭症患者 2020-11-28 03:52

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 comp

6条回答
  •  一向
    一向 (楼主)
    2020-11-28 04:33

    In a linked question, there's some Objective-C code to get the mime type for a file URL. I've created a Swift extension based on that Objective-C code to get the mime type:

    import Foundation
    import MobileCoreServices
    
    extension URL {
        var mimeType: String? {
            guard self.pathExtension.count != 0 else {
                return nil
            }
    
            let pathExtension = self.pathExtension as CFString
            if let preferredIdentifier = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, nil) {
                guard let mimeType = UTTypeCopyPreferredTagWithClass(preferredIdentifier.takeRetainedValue(), kUTTagClassMIMEType) else {
                    return nil
                }
                return mimeType.takeRetainedValue() as String
            }
    
            return nil
        }
    }
    

提交回复
热议问题