wbf
4 months ago

MethodNotAllowedHttpException with Laravel5.4 when uploading files bigger than 1MB online, but it works perfectly on localhost

Posted 4 months ago by wbf

I'm using Laravel 5.4 for my website, and I'm getting a problem when the user tries to upload files bigger than 1MB. This problem doesn't appear on localhost. The error I get is nothing related to the file size, but it's a MethodNotAllowedHttpException in RouteCollection.php line 251. The method I'm using to upload the files is 'PUT'.

I've tried the " $request->file('postpic')->storeAs('/public/post_pic/',$filenametostore);" and the "Storage::putFileAs('public/post_pic', $request->file('postpic'), $filenametostore);" but none of them worked online for files bigger than 1MB.

I also checked the php options on cpanel, and they are set to accept files size of 50MB (max_file_size), a memory_limit of 768MB, a post_max_size of 128MB.

I also contacted the hosting server, and after opening a ticket to see what's wrong, I've got an answer that there is a programming bug and the server's configuration is fine, and they suggested to contact Laravel for support.

I've spent two days looking for an online solution, but I didn't get any good results about this topic, that's why I'm asking this question here, and I would really appreciate any assistance to solve it.

Finally, I've noticed that when I upload the same 9MB file offline Vs online: looking at the Network tab in the browser, I could find that there were updates about the user's ID and the file Id when uploading this file offline, but online, the network freezes for a while without showing any updates, then the error page appears. When using a smaller file (less than 1.2MB) everything works normally online and offline.

My route is:

Route::POST('/post/{id}/store',[
    'as' => 'storepost',
    'uses' => '[email protected]'
]);

My controller function is:

public function store(Request $request, $id, loggeduser $lu)
    {
        $this->validate($request,[
            'body' => 'required',
            'postpic' => 'nullable|max:40000'
        ]);
        
        $profile = profile::find($id);
        $isvideo = 0;
            
        if ($request-> hasfile('postpic')){

            $filenamewithext = $request->file('postpic')->getClientOriginalName();
            $filename = pathinfo($filenamewithext,PATHINFO_FILENAME);
            $extension = $request->file('postpic')->getClientOriginalExtension();
            $filenametostore = '1_'.$filename.'_'.time().'.'.$extension;
            $orgimage = Storage::disk('local')->putFileAs('public/post_pic', $request->file('postpic'), $filenametostore);
                
            if(substr($request->file('postpic')->getMimeType(), 0, 5) == 'image') {
                //Resize image here
                    
                $imagepath = public_path('storage/post_pic/'.$filenametostore);
                $img = Image::make($imagepath)->resize(700, 400, function($constraint) {
                    $constraint->aspectRatio();
                });
                $img->save($imagepath);
                    
            }elseif(substr($request->file('postpic')->getMimeType(), 0, 5) == 'video') {
                //it's a video                    
                $isvideo=1;
            }
        }else{
            //there is no photo
            $filenametostore = '0';
        }
            
        $logged_user_id = $lu->userid;
        $post = new post();
        $post->post_pic = $filenametostore;
        $post->isvideo = $isvideo;
        $post->body = $request->input('body');
        $post->user_id = $logged_user_id;
        $post->profile_id = $id;
        $post->save();

        return redirect('/profile/'.$id)->with('success','Your story is alive..');
        

    }

The blade file:

{!!Form::open(['action'=>['[email protected]','id'=>$profile->id], 'method'=>'POST', 'enctype'=>'multipart/form-data', 'files'=>true])!!}

{!! csrf_field() !!}

<input id="postpic" type="file" name="postpic" class="btn btn-primary" style="width:100%;"/>

{{Form::textarea('body','',['class'=>'form-control','required','placeholder'=>'Post a story on your profile..'])}}

{{Form::submit('Post',['class'=>'btn btn-success'])}}

{!!Form::close()!!}

Thank you in advance.

Please sign in or create an account to participate in this conversation.