Thomas Clayson
Thomas Clayson

Reputation: 29925

Best way to validate an upload form?

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

Using the mime 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

Upvotes: 0

Views: 264

Answers (3)

RobertPitt
RobertPitt

Reputation: 57268

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;
}

Upvotes: 0

Thomas Clayson
Thomas Clayson

Reputation: 29925

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.

Upvotes: 1

whlk
whlk

Reputation: 15635

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.

Upvotes: 0

Related Questions