php settings on local vs production server (laravel) - php

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() }}" />

Related

Upload file using Laravel 5.3

I want to upload file in my app.
This is the blade file .
<form action="/fileUploader " files="true" method="post" role="form" name="file" id="chan" >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="panel panel-default">
<label>Please Select a File to Upload</label>
<input type="image">
<button type="submit" name ="Upload_File">Upload File</button>
</div>
</form>
This is my controller file
public function viewFile()
{
return View::make('/fileUploader');
}
public function showfileupload(Request $request)
{
$file = $request -> file('image');
// show the file name
echo 'File Name : '.$file->getClientOriginalName();
echo '<br>';
// show file extensions
echo 'File Extensions : '.$file->getClientOriginalExtension();
echo '<br>';
// show file path
echo 'File Path : '.$file->getRealPath();
echo '<br>';
// show file size
echo 'File Size : '.$file->getSize();
echo '<br>';
// show file mime type
echo 'File Mime Type : '.$file->getMimeType();
echo '<br>';
// move uploaded File
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
This is the web.php file
Route::get('/fileUploader', 'channelController#viewFile');
Route::post('/fileUploader', 'channelController#showfileupload');
I'm getting an error called FatalThrowableError in channelController.php line 48:
Call to a member function getClientOriginalName() on null.
How can I solve this problem
Most likely, you are trying to call a method - getClientOriginalName() - on a object that doesn't exist, so it's null. That jives with the error message you are seeing.
I'm not sure why, but we can start working backwards. Let's use an if statement with the hasFile() method to verify that a file is actually present on the request before attempting to move() it.
if ($request->hasFile('image')) {
$file->move($destinationPath,$file->getClientOriginalName());
}
If you implement the above, does the error still exist?
Here are the Laravel 5.3 Docs on file uploads. It may give you some more ideas.
If you're finding that users are posting files and hasFile() is still returning boolean FALSE, then you may want to go digging into the php.ini file and take a look at the Post_max_size or upload_max_size values to make sure that we aren't blocking large uploads.
VIEW
{!! Form::open(['route'=>'fileUploader', 'id'=>'chan', 'files' => true] )!!}
<div class="panel panel-default">
<label>Please Select a File to Upload</label>
<input type="file" name="image">
<button type="submit">Upload File</button>
</div>
{!! Form::close()!!}
ROUTES
Route::get('/fileUploader', 'channelController#viewFile');
Route::post('fileUploader', array(
'as' => 'fileUploader',
'uses' => 'channelController#showfileupload',
));
CONTROLLER
public function showfileupload(Request $request){
$file = $request -> file('image');
dd($file); // This work well for me and return information about the image
}
Do copy and past! i hope it work, let me know any error and result!

Laravel Dropbox Fire Upload- InvalidArgumentException in Checker.php line 22:

I want to upload file with dropbox Api using Laravel. I have used following Code for uploading from my drive to dropbox. But while Uploading I'm getting following Error:
InvalidArgumentException in Checker.php line 22:
'inStream' has bad type; expecting resource, got Illuminate\Http\UploadedFile
If anyone faced or solved the problem, help me to solve it please.
Here is my controller code:
public function dropboxFileUpload()
{
$Client = new Client(env('DROPBOX_TOKEN'), env('DROPBOX_SECRET'));
$dropboxFileName='';
$file = Input::file('image');
$size = Input::file('image')->getSize();
$name = Input::file('image')->getClientOriginalName();
$dropboxFileName = '/'.$name;
$Client->uploadFile($dropboxFileName,WriteMode::add(),$file, $size);
$links['share'] = $Client->createShareableLink($dropboxFileName);
$links['view'] = $Client->createTemporaryDirectLink($dropboxFileName);
print_r($links);
}
Here is the route part:
Route::post('dropboxFileUpload', 'ImportController#dropboxFileUpload');
And here is the view part:
<form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="{{ URL::to('dropboxFileUpload') }}" class="form-horizontal" method="post" enctype="multipart/form-data" >
<input type="file" name="image" />
{!! Form::token(); !!}
{!! csrf_field() ; !!}
<button class="btn btn-primary">Import File</button>
</form>
Can you please try
$Client->uploadFile($dropboxFileName,WriteMode::add(),$file, $size, null, true);
It's used for indication if the file is for testing purposes. This will skip validation.
Second thing is to check if you create object of \Illuminate\Http\UploadedFile and not \Symfony\Component\HttpFoundation\File\UploadedFile

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.

TokenMismatchException in VerifyCsrfToken.php line 67 when uploading video

Picture 2 Form
Picture 3 Form
Hi,
I'm getting the error mentioned in the title when trying to upload a video using Laravel 5.2.
Images work correctly.
I've checked the PHP.ini settings of my MAMP server.
I'm using the form facade so I don't have to import token into my form.
I'm clueless, does anybody have suggestions what it might be?
<div class="container spark-screen">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Bestand uploaden</div>
<div class="panel-body">
{!! Form::open(
array(
'url' => 'uploads',
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true)) !!}
#include('uploadspanel.create_form')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
controller:
public function store(UploadRequest $request){
$extension = Input::file('file')->getClientOriginalExtension();
$filename = rand(11111111, 99999999). '.' . $extension;
Input::file('file')->move(
base_path().'/public/files/uploads/', $filename
);
$approved = $request['approved'];
$fullPath = '/public/files/uploads/' . $filename;
$upload = new Uploads(array(
'name' => $request['name'],
'format' => $extension,
'path' => $fullPath,
'approved' => $approved,
));
$upload->save();
$uploads = Uploads::orderBy('approved')->get();
return view('uploadspanel.index', compact('uploads'));
}
Make sure you have the token included in your form, go to your page and inspect it, you should see something like this:
<input name="_token" type="hidden" value="Th4yqxNa3w3ooVAxRcSgvMug7ZEPA6BtaUw4qRck">
if you don't then add it in your blade like this:
{{ Form::hidden("_token", csrf_token()) }}
Another issue you might have is in case you are submitting this form through an AJAX request, in that case, you would need to pass the token there too:
$.ajax({
url : '{{ route("your_route", optional_parameter) }}',
type : "post",
data : { '_token' : '{{ csrf_token() }}', 'var1' : var1 },
}).done(...)
It had to do with MAMP settings. figured it out when i echo php_info();
then on line 6 or 7 followed the path to my php.ini
then changed the inputs again with another editor, saved it.
retart MAMP server
and done
This happens when your file which you are uploading is smaller than the max upload size but more than the POST_MAX_SIZE.
The input is truncated at the POST_MAX_SIZE which means that the csrf token is lost.
You can change these values in php.ini file.

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

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()}}

Categories