1

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 class="form-group">
                    <label>Evidence Activity</label>
                    <input type="file" name="file" class="form form-control" 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.

slvhxia
  • 47
  • 6
  • Hi buddy, does [this](https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size) answer your question? – Huy Phạm Mar 17 '22 at 10:50
  • Does this answer your question? [Change the maximum upload file size](https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size) – M. Eriksson Mar 17 '22 at 11:04
  • Another ways maybe, it's work but I won't use that – slvhxia Mar 17 '22 at 11:06

2 Answers2

0

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.

Valeriu Ciuca
  • 2,084
  • 11
  • 14
0

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.

Sohan Arafat
  • 93
  • 2
  • 16