Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wbf's avatar
Level 1

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

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' => 'PostsController@store'
]);

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'=>['PostsController@store','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.

0 likes
34 replies
tisuchi's avatar

If you have a chance to change the memory_limit in php.ini file, then increase that 128 MB from 68 MB.

1 like
wbf's avatar
Level 1

@TISUCHI - Hello and thank you for the fast reply :) actually the memory limit is 768MB

tisuchi's avatar

@wbf

Strange!

Then you may apply validation rules and set file size.

1 like
Snapey's avatar

You need to look at both

  • upload_max_filesize
  • post_max_size

as well. Your problem is that the file takes up all the available post size and the fields (including method and csrf) are cut from the post, causing this strange issue with method not allowed

Snapey's avatar

Reading your question further, I would create a route that returns phpinfo() and check the values there rather than in cpanel.

wbf's avatar
Level 1

@SNAPEY - thanks Snapey for replying, but the file I'm trying to upload is way smaller than the post_max_size and the upload_max_filesize... I think I'll try doing what you suggested by creating a route that returns the phpinfo and see what's there, then I'll come and tell you what I found :) thanks a lot for your help guys, I really appreciate it

wbf's avatar
Level 1

@SNAPEY - Well, after checking the phpinfo, I found that everything is all right there with the sizes :s that's really weird...

wbf's avatar
Level 1

@TISUCHI - Do you mean to apply a validation rule in the blade page?

tisuchi's avatar

@wbf

Apply form validation in the controller and define a maximum file size. That's what I mean.

1 like
wbf's avatar
Level 1

@TISUCHI - Sorry if I didn't get you right, but is it something other than

$this->validate($request,[ 'body' => 'required', 'postpic' => 'nullable|max:40000' ]);

MaverickChan's avatar

what do you mean

The method I'm using to upload the files is 'PUT'.

and your controller looks like using PUT method, But , uploading a image file is quite simple POST method.

Also , in your blade file , there is no trace of a hidden input to change the method to PUT.

Maybe your online server has some special rule of post max size limit

wbf's avatar
Level 1

@MAVERICKCHAN - Oh, I'm sorry... yes I was using a PUT, then I changed it to POST just before posting it here because someone told me that the method POST should be used when dealing with files upload... Actually neither POST nor PUT worked online, but both of them worked on localhost. And about the post max size rule, I've contacted the support team of the hosting server, and they said that they have no such rule.

Snapey's avatar

Some resources here

https://stackoverflow.com/questions/6135427/increasing-the-maximum-post-size

Validation might be a requirement but as you don't even reach the controller, its not a consideration

Having said that... you have trapped (dd) the invocation of the controller method?

Your method not allowed error could also come as a result of redirection.

I have seen this when there are two forms. Submit form 1, returns form 2 directly, and not as a redirect. When there is any sort of validation error, Laravel tells the browser to redirect with a GET to the Form2 URL - resulting in this error.

wbf's avatar
Level 1

@SNAPEY - You're absolutely right! I've tried actually the dd($request) at the beginning of the store function, but it didn't work; it just took me to the same error page with the URL /post/id/store... the weird thing is when the file is smaller than 1MB, it uploads normally then return to /profile/id. while on the localhost, no matter how big the file is (I mean smaller than the max_size), it works normally. I think I'll try to re-upload the whole thing from the beginning and see what happens then...

wbf's avatar
Level 1

@SNAPEY - Just to make sure that everything is right with forms, I've created a testing page with no scripts, no forms, nothing except the posting area, and I'm still having the same problem. I would really thank you again for your help. I really appreciate it

Snapey's avatar

Earlier you said

And about the post max size rule, I've contacted the support team of the hosting server, and they said that they have no such rule

But you said you checked the values from phpinfo and they were fine. I assume this included post_max_size ?

Can you try some different files to upload?

wbf's avatar
Level 1

@SNAPEY - I meant by "they said that they have no such rule" is that they don't have restrictions to the uploaded files size from their side... It's up to the user to modify the php values. the post_max_size from the phpinfo() showed 64M, which is the same value I chose. and I've also added these lines to the .htaccess file:

php_value session.gc_maxlifetime 10800

php_value max_input_time 10800

php_value max_execution_time 10800

php_value upload_max_filesize 64M

php_value post_max_size 64M

as mentioned on stackoverflow 's link that you shared.

and for different file types, they are affected the same way... smaller than 1M works fine... 2M files return to the error page :s

wbf's avatar
Level 1

Hmm... I think it's about timing? maybe? well, I tried to upload a file of 1.10 MB, and it failed... then, I removed the loading script, and tried to upload the same file again, and it worked. then, I switched to upload a bigger file and it failed again.

I have the following:

max_input_time = -1

max_input_vars = 1000

output_buffering = 4096

post_max_size = 64M

realpath_cache_size = 4096K

realpath_cache_ttl = 112

upload_max_filesize = 256M

user_ini.cache_ttl = 300

memory_limit = 128M

max_input_nesting_level = 64

max_file_uploads = 50

max_execution_time = 300

Sorry to mention all these, but frankly speaking I don't know the role of each of them and if they may affect the results.. as far as I know, I think the values are okay, right?

Snapey's avatar

You have two upload_max_filesize

upload_max_filesize = 256M

upload_max_filesize = 300
wbf's avatar
Level 1

Sorry, it's my bad... updated now: user_ini.cache_ttl = 300 not upload_max_filesize=300...

siangboon's avatar

Try disable the mod_security setting in the shared hosting.

wbf's avatar
Level 1

@SIANGBOON - Hello and thanks for joining. I'll try to disable the mod_security and let you know what happens

wbf's avatar
Level 1

Good news.. after disabling the https finally, I'm getting this error:

This site can’t be reached
The connection was reset.
The connection to the server was reset while the page was loading.

Try:
Checking the connection
Checking the proxy and the firewall
Running Windows Network Diagnostics
ERR_CONNECTION_RESET
Check your internet connection.
Check any cables and reboot any routers, modems, or other network devices you may be using.

So that means there is something interrupting the upload to the server and I think it is not related to the hosting server... I'll try to switch to another connection and test it again

wbf's avatar
Level 1

No, nothing... I'm still getting the same page. do you think it is something related to my configuration? or it is something that the hosting server should take care of?

jlrdw's avatar

What is your host. I have never had trouble uploading to a shared host.

This works perfect for Godaddy, I know but what client had:

View:

<div style="margin-right:auto;margin-left:0px; width:900px;">


    <form action='add' method='post' enctype="multipart/form-data">
        <table style="border:none; width: 700px;">
            <tr>
                <td>dogpic:</td>
                <td>
                    <input name="ufile" type="file" id="ufile" size="50" /></td>
                </td>
            </tr>
            <tr>
                <td>dogname:</td>
                <td>
                    <input type="text" name="dogname" id="dogname">
                </td>
            </tr>
            <tr>
                <td>sex:</td>
                <td>
                    <input type="text" name="sex" id="sex">
                </td>
            </tr>

/////  more fields

controller:

    public function add()
    {
        if (isset($_POST['submit'])) {
            $lid = DB::table('dc_dogs')->count();
            $lid = $lid + 1;
            $file = Request::file('ufile');
            $file_name = $file->getClientOriginalName();
            $file_ext = $file->getClientOriginalExtension();

            $fileInfo = pathinfo($file_name);
            $filename = $fileInfo['filename'];
            $newname = $filename . $lid . "." . $file_ext;
            $destinationPath = ASSET . 'upload/imgdogs';
            $file->move($destinationPath, $newname);

            $dogpic = $newname;

            $dogname = ucfirst(Request::input('dogname'));
            $sex = ucfirst(Request::input('sex'));
            $comments = Request::input('comments');
            $adopted = !empty(Request::input('adopted')) ? '1' : '0';
            $lastedit = date("Y-m-d H:i:s");


            $postdata = array(
                'dogpic' => $dogpic,
                'dogname' => $dogname,
                'sex' => $sex,
                'comments' => $comments,
                'adopted' => $adopted,
                'lastedit' => $lastedit
            );

            DB::table('dc_dogs')->insert($postdata);
        }
////  otherwise show add form

Where ASSET is:

define('ASSET', realpath(dirname(__FILE__)). DS . 'assets' . DS);

Of course images are web optimized and not huge in file size prior to uploading.

I have never received any error.

Also, make sure you are correctly set up:

https://laracasts.com/discuss/channels/laravel/next-issue-fonts-and-images-arent-visible

In actual app I strip_tags on text fields, showed simplified version here.

wbf's avatar
Level 1

@SIANGBOON - well exactly... that's what I saw when I was searching how to disable the Mod_security, but there is no such thing on my cpanel... I'm on a2hosting

wbf's avatar
Level 1

@JLRDW - Thanks for joining. My host is a2hosting.

About the files upload and the directories that you have mentioned, I'm not having any problem uploading any kind of files that are under a certain size (1.1MB), but when they are bigger the error starts showing. I'm not having this problem on localhost, so that's why it should be something to do with the hosting server configuration. I checked the server's configuration and contacted the support team, but everything is all right there... they actually told me that they give permission to apply your configuration through htaccess. this is how my htaccess looks like:

RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

php_value session.gc_maxlifetime 10800
php_value max_input_time 10800
php_value max_execution_time 10800
php_value upload_max_filesize 64M
php_value post_max_size 60M
php_value memory_limit 64M


<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    Options +FollowSymLinks
    Options -Indexes 

    
    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ / [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    
</IfModule>

But the weird thing is when I change the post_max_size to 1M, the 1.1MB files are still being uploaded!

wbf's avatar
Level 1

@SIANGBOON - You were actually right :D. I've contacted the support and they are working to disable the mod_security. Let's see if this can happen. Anyway, what do you think about trying to upload the post using ajax? would that help in solving anything? I think it won't since the normal upload method doesn't work...

Next

Please or to participate in this conversation.