Sam's avatar
Level 4

CSV File Upload Request Validation

Hi all,

Wondering if someone could lend a hand here. I have a form field that is used for a CSV file upload, all goes through fine however I have dropped in validation via a From Request with the following rules:

public function rules()
    {
        return [
            'csv_import'      => 'required|mimes:csv',
        ];
    }

Unfortunately validation completely fails despite using a CSV file.

Any help would be much appreciated!

0 likes
9 replies
rodrigo.pedra's avatar

As CSV is basically a text-file, so its mime-type is checked against the file extension. Does your file have a .csv extension?

If you want to allow .txt extension as well, change your validation rule to this:

return [ 'csv_import' => 'required|mimes:csv,txt' ];
4 likes
Sam's avatar
Level 4

Hi @rodrigo.pedra

Yes the file does have a .csv extension. Using getMimeType() function on the file upload returns 'text/csv'

Thanks

1 like
rodrigo.pedra's avatar

I do a csv upload in my app and it works fine with that rule...

Does your <input type="file"> has the same name attribute than validation?

I mean:

<input type="file" name="csv_import">

Also, when sending files through a HTML form you need to add the enctype="multipart/form-data" attribute to the <form> tag, for example:

<form action="{{ route( 'my-route' )  }}" method="POST" accept-charset="UTF-8" enctype="multipart/form-data">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">

    <input type="file" name="csv_import">

    <button type="submit">Send it</button>
</form>
1 like
Sam's avatar
Level 4

Thanks for your response and apologies for my delay in getting back to you. I have gone over my code and ensured it works correctly however it would seem that my client was trying to upload a file with the following filename items (1).csv.

This was failing the validation, for obvious reasons (I think). With the above in mind would I be best of validating the filename on the front-end (javascript) or via laravel, if it's the latter what approach should I take?

Thanks

rodrigo.pedra's avatar
Level 56

That is it! I tried it myself and if you only test for mimes:csv it will fail when the filename has an space.

What I did to pass validation was to use required|mimes:csv,txt.

But as you already suggested you would need to do some manual validation.

Front-end validation is a good thing regarding the user experience, but you can never trust solely in a validation that ran in a machine different than your server. If you are going to do any javascript validation, always validate it again in the server.

In your case, as it is an exception regarding an unusual filename, not an wrong format, I would relax the mime-type validation using mimes:csv,txt and then use a package to parse the CSV file, generally this packages raises an exception when the format is not valid.

Two great packages for dealing with CSV are:

  1. [ https://github.com/Maatwebsite/Laravel-Excel ]
  2. [ http://csv.thephpleague.com/ ]

Hope it helps.

11 likes
technoigniters's avatar

@rodrigo.pedra your solution did not work for me. I have added csv file validation code in controller-action file as: 'upload_csv_file' => 'required|mimes:csv|max:10000',

I am uploading csv file generated from google but there is no success. System says file is not in csv format. Any suggestion? Please share your views. Thanks a lot.

rodrigo.pedra's avatar

@technoigniters do you realize you are replying to a 7 years old thread?

Laravel changed a lot since then, and I probably don't have the views I used back then anymore.

Please open a new thread and explain what your actual issue is. You can link to this thread, but please add more details on your current issue.

rodrigo.pedra's avatar

Also, the accepted answer from 7 years ago suggests using mimes:csv,txt for validation and not just mimes:csv as you say you are using.

Have you considered following the accepted answer advice?

hasen39's avatar

i have solved this issue using laravel file validation


 use Illuminate\Validation\Rules\File;
  

'file' => [File::types(['csv'])]
1 like

Please or to participate in this conversation.