Search code examples
phpfile-uploadfat-free-framework

file validation in fat-free framewirk


I want to upload multiple files of any types, and upon failing validation check, I want to display validation messages too.

I have been following f3 doc. and this code only skips the file movement if the file fails validation check.

$f3->route('GET|POST|PUT /uploadfile',
function($f3) use($db){
 $f3->set('UPLOADS','uploads/');  

    $overwrite = false; Default: false
    $slug = true; 
    $web = \Web::instance();
    $files = $web->receive(function($file){  

            if($file['size'] > (2 * 1024 * 1024)) 
                return false;  

            // everything went fine, hurray!
            return true;  
        },
        $overwrite,
        $slug
    );     

}
);

How do I display message to user on validation fail? Any help is very much appreciated. Thanks.


Solution

  • That's up to you how you handle this. Basically, when uploading multiple files, you probably want to track the errors you produce in an array, so maybe like this:

    $web = \Web::instance();
    $errors=[];
    $files = $web->receive(function($file) use ($errors) {  
            if($file['size'] > (2 * 1024 * 1024)) {
                $errors[ $file['name'] ] = 'File too large';
                return false;  
            }
            return true;  
        },
        $overwrite,$slug
    );     
    if ($errors) {
        $f3->status(413);
        echo json_encode($errors);
        exit();
    }