Home > Net >  Input File Maximum Size
Input File Maximum Size

Time:03-18

I'm new at Laravel. How can I limit the size of input file to 5mb?

This is my controller :

public function add_project_activity(Request $request){
    $id_rotation        = $request->id_rotation;
    $input_activity     = $request->activity_name;
    $input_detail       = $request->detail_activity;
    $input_file         = $request->file;

    $nik = Sentinel::getUser()->nik;

    if (!empty($request->file) && $request->hasFile('file')) {
        $new_id         = self::check_id();

        $filename       = $input_file->getClientOriginalName();
        $new_filename   = "evidence_" . $new_id . "-" . $filename;
        $upload_file    = $input_file->storeAs('public/accelerate/'.$nik.'/',$new_filename);

        $submit_data = AccelerateProjectActivity::create([
            'status'            => 'draft',
            'activity_name'     => $input_activity,
            'detail_activity'   => $input_detail,
            'evidence'          => $new_filename,
            'month'             => $request->month,
            'id_rotation'       => $id_rotation,
        ]);

        return redirect()->back()->with('success', 'your message,here');
    } elseif(empty($request->file)){
        $submit_data = AccelerateProjectActivity::create([
            'status'            => 'draft',
            'activity_name'     => $input_activity,
            'detail_activity'   => $input_detail,
            'month'             => $request->month,
            'id_rotation'       => $id_rotation,
        ]);
    }

    return redirect()->back();
}

This is my view of input :

<div >
                    <label>Evidence Activity</label>
                    <input type="file" name="file"  accept="application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/pdf">
                </div>

Thank you, I'm confused at this, how to place the validator in my controller. Hope you're answer my question. Greetings.

CodePudding user response:

There is a more simple setup . You can edit it in htaccess. Or you can set upload limit in php.ini file. Depending on which system you are working you can have different setting. But you should take a look at the configurations.

CodePudding user response:

You could try setting the upload_max_size on the fly in the add_project_activity() controller method.

public function add_project_activity(Request $request) {
    @ini_set('upload_max_size' , '5M');
    ...

https://www.php.net/manual/en/function.ini-set.php

OR edit php.ini file

; Maximum allowed size for uploaded files.
upload_max_filesize = 5M

; Must be greater than or equal to upload_max_filesize
post_max_size = 5M

OR edit your .htaccess file

php_value upload_max_filesize 5M
php_value post_max_size 5M

But these last 2 options will be global, for all your file upload situations.

  • Related