exception 'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException' Uploading large files laravel - php

In a view in laravel i have a form that upload a file:
<form id="file-submit" enctype="multipart/form-data" method="post" action="store">
{{ Form::token() }}
<input id="filename" name="filename" type="file" />
<input type="submit" value="Upload file" id="file-save" class="btn btn-create" />
</form>
In the route file i have:
Route::post('/store', 'MyController#upload');
There in the method, i process the file data.
This works in my local server, i upload it in production, so, this works when i upload files around 5kb, but if i try to upload a large file, around 4MB it breaks with this error:
production.ERROR: exception 'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException' in <laravel_instance>/protected/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:210
I have the same memory settings in php.ini file (memory_limit, post_max_size, excecution_time, max_input_time, upload_max_size)
Thanks

I suspect that it has something to do with you manually creating the form.
Try doing it like this:
Route:
Route::post('/store', [
'as' => 'store',
'uses' => 'MyController#upload'
]);
Form:
{{Form::open([
'id' => 'file-submit',
'enctype' => 'multipart/form-data',
'action' => 'store'
])}}
{{Form::file('filename')}}
{{Form::submit()}}
{{Form::close()}}

Related

file input throws an error when form is submitted

I run into a problem when trying to upload images to the server using a symfony form as follows:
$form = $this->createFormBuilder($image)
->add('full', FileType::class, array('label' => 'Imagen', 'multiple' => true))
->add('save', SubmitType::class, array('label' => 'Guardar'))
->getForm();
Then in the html:
{{ form_start(form) }}
{{ form_row(form.full) }}
{{ form_end(form) }}
When I inspect the html I get:
<form name="form" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label required" for="form_full">Imagen</label>
<input type="file" id="form_full" name="form[full][]" required="required" multiple="multiple" />
</div>
<div class="form-group">
<button type="submit" id="form_save" name="form[save]" class="btn-default btn">Guardar</button>
</div>
<input type="hidden" id="form__token" name="form[_token]" value="vZjUzyZCsbsx5TmfWiljncIi1pPymfod_jezOOKgK_k" />
</form>
When I get the value of the file in the controller I have this:
originalName="myPicture.jpg"
mimeType="image/jpeg"
size=8450
erro=0
*SplFileInfo*pathName="E:\xamp\tmp\php51B5.tmp"
*SplFileInfo*fileName="php51B5.tmp"
So far, well, but in other cases of other images with the same format (jpeg), it happens that I get this:
originalName="myPicture.jpg"
mimeType="application/octet-stream"
size=0
erro=1
*SplFileInfo*pathName=""
*SplFileInfo*fileName=""
As you can see it does not recognize the Mimetype that should be image / jpeg, and it shows that an error occurs but it does not say which one?
As RiggsFolly said, the problem is that the error = 1 means that the file you are trying to upload is larger than the limit set in the php.ini of the server, changing this parameter is solved:
php.ini:
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=8M

php settings on local vs production server (laravel)

I have made a laravel project where users can upload images. I have tested the project on local server and found nothing wrong. After uploading the project to the production server I found that, when a user tries to upload images that are greater than 1.5mb size, it shows error TokenMismatchException in VerifyCsrfToken.php line 67 although i have
csrf token in my form. After couple of searching I found that some people are suggesting to change the two things in php.ini file and restart the nginx server. So I have changed the post_max_size = 40M upload_max_filesize = 40M and also changed the max_execution_time = 0 which means unlimited execution time. Then I run this command sudo service nginx restart and php artisan up and tried to upload an image which is 21MB size. When I pressed the submit button, It took sometime to upload the image and eventually threw the token mismatch exception. I am using ubuntu 16.04 for local tests. Any solution to this problem please?
I am sharing the codes:
view:
{!! Form::open(['url'=> "pro/{$user->id}/upload",'files'=> 'true', 'class'=> 'form-horizontal']) !!}
<input id="filebutton" name="image" class="input-file" type="file">
<input name="title" class="form-control" type="text" required="">
<button type="submit" class="btn btn-danger btn-block btn-flat">Upload</button>
{!! Form::close() !!}
controller:
public function save($id, PortfolioRequest $request)
{
$pro = User::findOrFail($id);
$file = $request->file('image');
$original_path = public_path('uploads/portfolio/original/');
$file_name = str_random(64).'_'.$request->title.'_user_'. $pro->id . '.' . $file->getClientOriginalExtension();
Image::make($file)
->resize(750,null,function ($constraint) {
$constraint->aspectRatio();
})
->save($original_path . $file_name);
$portfolio = Portfolio::create([
'user_id' => $id,
'image' => $file_name,
'title' => $request->title
]);
return redirect("pro/{$id}/portfolio");
}
the generated form:
<form method="POST" action="www.xxxx.com/pro/3/upload" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data"><input name="_token" type="hidden" value="EsH8KaSSoXovzjZ0RnWWi7eEwNWNgYlBVRm7yUYr">
Try adding this to your <header>:
<meta name="csrf-token" content="{{ csrf_token() }}" />

FileNotFoundException in laravel

In laravel i am making an application that uploads a file and the user can download that same file.
But each time i click to upload i get this error.
FileNotFoundException in File.php line 37: The file
"H:\wamp64\tmp\phpF040.tmp" does not exist
my view code is this:
#extends('layouts.app')
#section('content')
#inject('Kala','App\Kala')
<div class="container">
<div class="row">
#include('common.errors')
<form action="/addkala" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" name="name">
<input type="text" name="details">
<input type="file" name="photo" id="photo" >
<button type="submit">submit</button>
</form>
</div>
</div>
#endsection
and my controller
public function addkalapost(Request $request)
{
$rules = [
'name' => 'required|max:255',
'details' => 'required',
'photo' => 'max:1024',
];
$v = Validator::make($request->all(), $rules);
if($v->fails()){
return redirect()->back()->withErrors($v->errors())->withInput($request->except('photo'));
} else {
$file = $request->file('photo');
$fileName = time().'_'.$request->name;
$destinationPath = public_path().'/uploads';
$file->move($destinationPath, $fileName);
$kala=new Kala;
$kala->name=$request->name;
return 1;
$kala->details=$request->details;
$kala->pic_name=$fileName;
$kala->save();
return redirect()->back()->with('message', 'The post successfully inserted.');
}
}
and i change the upload max size in php.ini to 1000M.
plz help
im confusing
I'll recommend you using filesystems for that by default the folder is storage/app you need to get file from there
if your file is located somewhere else you can make your own disk in config/filesystems e.g. 'myDisk' => [
'driver' => 'local',
'root' =>base_path('xyzFolder'),
],
and you can call it like
use Illuminate\Support\Facades\Storage;
$data = Storage::disk('myDisk')->get('myFile.txt');
this is obviously to get file and you can perform any other function by following laravel docs.

Input file in laravel 5.2?

I'm trying to upload a file, but it fails when the request lands to the controller.
With fails i mean that if i try $request->hasFile("filename") always returns false.
Is there some specific field that I have to specify in the view?
This is a snippet of the view:
<body>
<form action="{{url('dev/tester')}}" method="POST">
{{csrf_field()}}
<input type="file" name="file">
<button type="submit">Test</button>
</form>
</body>
And here is the controller
class Tester extends Controller
{
public function index(Request $request)
{
if($request->hasFile('file'))
{
dd('Got the file');
}
dd('No file');
}
public function testView()
{
return view('tests.file_upload');
}
}
I always get returned 'No file'.
Any clue? I've even check the php.ini to see if there was a size limitation but it's all set to 32M as MAMP's pro default settings...
Check if you may have forgotten to add enctype="multipart/form-data" in form
You must enabling upload form to your form,
there is 2 ways to do it :
By using HTML
<form action="{{url('dev/tester')}}" method="post" enctype="multipart/form-data">
By using laravel Form & HTML (https://laravelcollective.com/docs/5.2/html)
{!! Form::open( [ 'action' => url( 'dev/tester' ), 'method' => 'post', 'files' => true ] ) !!}
// Your form
{!! Form::close() !!}
This should work like a charm!
Try adding the enctype="multipart/from-data" to your form, then it should work!

Laravel - upload multiple files

I was searching for some tutorials on how to upload multiple files, for example this: http://tutsglobal.com/discussion/301/how-to-upload-multiple-images-in-laravel-4
There is a {{ Form::file('images[]', ['multiple' => true]) }} line that should make from input for selecting multiple files. But the problem is that I can select only one file and not more. What could I do to be able to upload multiple files?
This is in my View:
{{ Form::open(['action' => 'AdminController#postProject', 'files' => true, 'enctype' => 'multipart/form-data', 'class' => 'projectform']) }}
{{ Form::file('images[]', ['multiple' => 'multiple']) }}
{{ Form::close() }}
This is HTML output:
<input multiple="multiple" name="images[]" type="file">
I just found that I can select multiple files with holding down SHIFT key, but I'd like to be able select files separately, one by one.
Hint: maybe its helpful for you.
<form action="demo_form.asp">
Select images: <input type="file" name="img" multiple>
<input type="submit">
</form>

Categories