anubhav09's avatar

Unable to upload the text file

I am trying to upload the text file having extension .txt containing some html tags also because of which my validation rule does not allow to upload the file.

protected function validator(array $data, $id = null)
{
    $rules = array();
    $rules['class_file'] = 'required|file|mimes:txt';
    return Validator::make($data, $rules);
}

how can i update the above rule so that i can upload the .txt extension file containing html code in it.

0 likes
1 reply
bheath's avatar

It is probably mime. It tries to read the file and deduce what the mime type is, which can be different than the file extension. It also doesn't seem to work 100% in my experience.

The only way I got this work was to create a custom validation

Example:

        $validator->extend('txt_file', function ($attribute, $value, $parameters) {
            $ext = strtolower($value->getClientOriginalExtension());
            return ($ext == 'txt');
        });

Please or to participate in this conversation.