开发者

Best way to validate an upload form?

Which would be the best way to validate an upload form?

Using the mime开发者_JAVA技巧 type at the moment, but that's not quite working - can't upload mpegs even though am looking for video in the mime type.

Thank you

Tom


This seems to work:

switch (strtolower($_FILES["file"]["type"])){
        case "application/msword":
        case "application/pdf":
        case "application/vnd.ms-excel":
        case "application/vnd.ms-powerpoint":
        case "application/zip":
        case "image/gif":
        case "image/jpeg":
        case "image/png":
        case "image/tiff":
        case "text/plain":
        case "video/mpeg":
        case "video/x-mpeg2":
        case "video/msvideo":
        case "video/quicktime":
            // do it
            break;
        default:
            // don't do it
            break;
    }

For anyone else this might help have a look at http://www.sfsu.edu/training/mimetype.htm for adding other mime types you might need to check.


I guess you want to check if an uploaded file is a valid video-file. So one thing you can check is the file extension (IE ".mpg" for mpeg video). Because no webframework known to me has an internal video-validation, you have to rely on some external program/library to check if the video file is really a video-file. Maybe FFMPEG is able to do this.


Try something like so:

$mime = strtolower($_FILES["file"]["type"]);
$parts = explode("/",$mime);

switch($parts[0])
{
    case 'video':
        //Video file, use $parts[1] to check the video subtype
    break;
    case 'image':
    break;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜