简体   繁体   中英

Laravel file upload: validation disk space

I have a file upload validation and would like to have an error message if the disk is full.

My validation looks like this at the moment:

$validator = Validator::make($request->all(), [
    'file' => [
        'required',
        'file',
        'mimes:' . $mimes,
        'max:' . config('myapp.max-file-size-kb')
    ]
], $messages);

I could use max for this but that would tell the user that the file is to large. But that is not really the problem.

Ok, I think that should work:

$validator = Validator::make($request->all(), [
    'file' => [
        'required',
        'file',
        'mimes:' . $mimes,
        'max:' . config('myapp.max-file-size-kb'),
        static function ($attribute, $file, $fail) {
            $free_space = disk_free_space( Storage::disk('uploads')->path('/') );
            if ($file->getSize() > $free_space) {
                $fail('Not enough disk space: ' . $free_space . ' bytes left.');
            }
        },
    ]
], $messages);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM