Wednesday, May 28, 2014

Validation uploaded File in php


if(isset($_FILES['uploaded_file'])) {
    $errors     = array();
    $maxsize    = 1000000;
    $acceptable = array(
        'image/jpg',
        'image/gif',
        'image/png',
        'application/pdf',
        'image/jpeg',
        
    );

    if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
        $errors[] = 'File too large. File must be less than 1MB.';
    }

    if(!in_array($_FILES['uploaded_file']['type'], $acceptable)) && (!empty($_FILES["uploaded_file"]["type"])))
    {
    $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
    }

if(count($errors) === 0) {
    move_uploaded_file($_FILES['uploaded_file']['tmpname'], '/store/to/location.file');
} else {
    foreach($errors as $error) {
        echo '<script>alert("'.$error.'");</script>';
    }

    die(); //Ensure no more processing is done
}
}