ryank30's avatar

Upload csv file issue

I am having difficulty uploading csv file. I have set up the rules for csv file but it keeps saying the type is not correct. Here below is my view:

@extends('layouts.app') @section('content')

{{$title}}

@if(count($collections) > 0) @foreach($collections as $collection)
  • {{ $collection->name }}
  • @endforeach @else

    Upload csv file

    {{ Form::open(['method' => 'post', 'url' => 'projects/detail/'.$id, 'class' => 'form-horizontal', 'role' => 'form']) }} has('file') ? ' has-error' : '' }}"> {{ Form::file('file')}} @if ($errors->has('file')) {{ $errors->first('file') }} @endif
                    <div class="form-group">
                        <div class="col-md-12">
                            <button type="submit" class="btn btn-primary">
                                Upload CSV
                            </button>
                        </div>
                    </div>  
                {{Form::close()}}
            </div>
        @endif
    </div>
    
    @stop

    Here below is my controller.

    public function storedetail(Request $request, $id){
            
        $file = $request->input('file');
    
        //$extension = File::extension($file);
        $values = array(  
            'file' => $file
        );
        $rules = array(
            'file' => 'required|mimes:csv'
        );
        $validator = Validator::make(
            $values,
            $rules
        );
    
        if ($validator->fails()){
            return redirect()->back()->withErrors($validator->errors());
        }
    
        dd($file);
    }
    

    Thanks in advance.

    0 likes
    14 replies
    Snapey's avatar

    you have to enable file uploads. include enctype="multipart/form-data" in your form element.

    ryank30's avatar

    How do I enable the form? I have tried enctype="multipart/form-data". But I get an error message saying the file field is required when I upload a csv file.

    ryank30's avatar

    @jlrdw I know to build a upload file system using php. I am talking about how to implement it on laravel 5.2 framework.

    ryank30's avatar

    @Snapey Thanks for your advice. When I uploaded my csv file with enctype="multipart/form-data", The value returned null.

    xirkus's avatar

    How about using $request->file()?

    https://laravel.com/docs/5.2/requests#files

    What happens if you drop the required from validation rules? It's kind of implicit that it's required anyway. Then just use the isValid() method (which I assume does something different to validation based on comments on the link above).

    Just guessing. Might help. Good luck

    xirkus's avatar

    How about using $request->file()?

    https://laravel.com/docs/5.2/requests#files

    What happens if you drop the required from validation rules? It's kind of implicit that it's required anyway. Then just use the isValid() method (which I assume does something different to validation based on comments on the link above).

    Just guessing. Might help. Good luck

    ryank30's avatar

    @xirkus Thanks for your tip. I just have tried $request->file() and worked. I also dd($file) to get info about the uploaded file. The file type was actually excel even though it's .csv file. I guess sometimes you will have to save as csv file again to make sure your file type is csv. Also I have discovered that csv is actually text/csv. So I put csv,txt,text for my mimes type and worked.

    Thanks guys again!

    xirkus's avatar

    Excel causing an issue?? Never!! Ha ha

    Always use a text editor for your CSVs. I learnt that the hard way too.

    Glad it's sorted.

    ryank30's avatar

    Thanks @xirkus I have one more question. I am trying to read the uploaded csv file by using SplFileObject class like below.

    $file = $request->file('file'); $fileObj = new \SplFileObject($file);

    But I don't know how to fetch the temp path of the file. Could you help me with this please?

    Thanks,

    Please or to participate in this conversation.