Arjun
Arjun

Reputation: 328

Laravel file not saved but recorded in database

I am accepting a file and some other parameters with it. I validate the file and the parameters and then I store the file while making a record in the database. Pretty standard stuff. The issue I have is that a majority of my files and records get saved but sometimes there exists a record in the database but there is no file associated with it saved. I have tried reproducing it but I haven't been able to. I don't know if the error is my code, or my server or if the user prematurely loses connection or some other issue of that nature.

I am running Laravel 7 on AWS Lightsail instance with Bitnami LAMP stack.

Store Method in Controller

public function store(StoreRequest $request)
{
    $filePath = $request
        ->file('file')
        ->storeAs(
            'path',
            Str::upper($request->input('param1')) .
                "_{$request->input('param2')}_{$request->input(
                    'param3'
                )}_{$request->input('param4')}_{$request->input(
                    'param5'
                )}_" .
                now()->format('Ymd_Hi') .
                ".{$request->file('file')->getClientOriginalExtension()}",
            'public'
        );

    Storage::setVisibility('public/' . $filePath, 'private');

    $record = Model::create(
        array_merge($request->all(), ['file' => $filePath])
    );

    return redirect()
        ->back()
        ->with('message', 'File submitted successfully');
}

Rules in StoreRequest

public function rules()
{
    $rules = [
        //rules for other parameters
        'filetype' => ['required', 'string'],
    ];

    if (request('filetype') === 'video') {
        $rules['file'] = [
            'required',
            'file',
            'mimetypes:video/*',
            'max:200000',
        ];
    } elseif (request('filetype') === 'image') {
        $rules['file'] = ['required', 'file', 'image', 'max:20000'];
    }

    return $rules;
}

I have 259 records on the database but I have only received 247 files. Where could the error lie? I have tried on my development environment but I haven't been able to reproduce an error like this. Is it something to do with my php.ini settings? Is it something that the user is causing? How can the file not be saved when I am saving it before the record gets stored in the database?

Upvotes: 0

Views: 823

Answers (2)

Ballard
Ballard

Reputation: 899

You are using Ymd_Hi which would not allow for any records saved in the same minute, perhaps use a timestring or include seconds too, but be warned, if you use seconds you may face the same issue!

Upvotes: 1

hmamun
hmamun

Reputation: 154

There are two options in pho.ini file. One is upload_max_filesize and another one post_max_size. While uploading the file I think it crosses the size that defined in ini file. If your video size 200 MB then upload_max_size should be more than or equal 200 MB. post_max_size is total size of form that going to be submitted. It is safer to set size more than upload_max_size.

Upvotes: 0

Related Questions