php - Laravel POST Token missmatch exception (file upload) -
i have piece of functionality creates popup bit of responsiveness (this works), have html code popup box (it's using jquery dialog show popup form between other html code, surrounded div makes invisible):
<form id="logform" method="post"> <select id='logoption'> <option value="0"> select logtype </option> <?php //sql in laravel style log types database $logtypes = db::table('time_log_types')->get(); foreach ($logtypes $logtype) { echo ' <option value="'. $logtype->id . '">' . $logtype->logtype.'</option>'; } ?> </select> <div id="illness" style="display:none"> <p>end date: <input type="text" id="enddate"></p> <p>doctor's note: <input type="file" id="doctorsnote"></p> </div> <input type="button" value="submit" id="submitbutton" style="display:none" /> </form>
the submit button on popup box (using jquery):
<script> $(document).ready(function() { //file upload $('#submitbutton').on('click', function() { var fd = new formdata(document.getelementbyid("logform")); //fd.append("customfield", "this data"); $.ajax({ url: ' ', type: 'post', data: fd, cache: false, contenttype: false, processdata: false }); }); }); </script>
i'm making sure post going correct place adding route in routes.php(i know posting correct place because have checked network tab when using inspect element in firefox):
route::post('calendar', 'logcontroller@upload');
finally code have in logcontroller:
<?php namespace app\http\controllers; use input; class logcontroller extends controller { public function upload() { $file = input::file('doctorsnote'); $destinationpath = 'c://xampp/htdocs/proofofconcept/savedimages'; // if uploads fail due file system, can try doing public_path().'/uploads' $filename = str_random(12); //$filename = $file->getclientoriginalname(); //$extension =$file->getclientoriginalextension(); $upload_success = $file->move($destinationpath, $filename); if( $upload_success ) { return response::json('success', 200); } else { return response::json('error', 400); } } }
the problem when click submit button 500 response , logged error message have no idea it:
> [2015-05-15 10:08:14] local.error: exception > 'illuminate\session\tokenmismatchexception' in > c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\http\middleware\verifycsrftoken.php:46 > stack trace: > #0 c:\xampp\htdocs\proofofconcept\laravel\app\http\middleware\verifycsrftoken.php(17): > illuminate\foundation\http\middleware\verifycsrftoken->handle(object(illuminate\http\request), > object(closure)) > #1 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): > app\http\middleware\verifycsrftoken->handle(object(illuminate\http\request), > object(closure)) > #2 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\view\middleware\shareerrorsfromsession.php(55): > illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) > #3 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): > illuminate\view\middleware\shareerrorsfromsession->handle(object(illuminate\http\request), > object(closure)) > #4 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\session\middleware\startsession.php(61): > illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) > #5 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): > illuminate\session\middleware\startsession->handle(object(illuminate\http\request), > object(closure)) > #6 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\cookie\middleware\addqueuedcookiestoresponse.php(36): > illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) > #7 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): > illuminate\cookie\middleware\addqueuedcookiestoresponse->handle(object(illuminate\http\request), > object(closure)) > #8 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\cookie\middleware\encryptcookies.php(40): > illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) > #9 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): > illuminate\cookie\middleware\encryptcookies->handle(object(illuminate\http\request), > object(closure)) > #10 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\http\middleware\checkformaintenancemode.php(42): > illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) > #11 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): > illuminate\foundation\http\middleware\checkformaintenancemode->handle(object(illuminate\http\request), > object(closure)) > #12 [internal function]: illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) > #13 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(101): > call_user_func(object(closure), object(illuminate\http\request)) > #14 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\http\kernel.php(115): > illuminate\pipeline\pipeline->then(object(closure)) > #15 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\http\kernel.php(84): > illuminate\foundation\http\kernel->sendrequestthroughrouter(object(illuminate\http\request)) > #16 c:\xampp\htdocs\proofofconcept\laravel\public\index.php(53): illuminate\foundation\http\kernel->handle(object(illuminate\http\request)) > #17 {main}
i have absolutely no idea going on, created code following guides around , far can tell should working i'm kind of new of , use figuring out
edit: added new field form include csrf token this:
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
and different stacktrace:
[2015-05-15 11:21:23] local.error: exception 'reflectionexception' message 'class app\http\controllers\logcontroller not exist' in c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\container\container.php:776 stack trace: #0 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\container\container.php(776): reflectionclass->__construct('app\http\contro...') #1 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\container\container.php(656): illuminate\container\container->build('app\http\contro...', array) #2 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\application.php(644): illuminate\container\container->make('app\http\contro...', array) #3 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\routing\controllerdispatcher.php(83): illuminate\foundation\application->make('app\http\contro...') #4 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\routing\controllerdispatcher.php(54): illuminate\routing\controllerdispatcher->makecontroller('app\http\contro...') #5 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\routing\route.php(204): illuminate\routing\controllerdispatcher->dispatch(object(illuminate\routing\route), object(illuminate\http\request), 'app\http\contro...', 'upload') #6 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\routing\route.php(134): illuminate\routing\route->runwithcustomdispatcher(object(illuminate\http\request)) #7 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\routing\router.php(701): illuminate\routing\route->run(object(illuminate\http\request)) #8 [internal function]: illuminate\routing\router->illuminate\routing\{closure}(object(illuminate\http\request)) #9 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(141): call_user_func(object(closure), object(illuminate\http\request)) #10 [internal function]: illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #11 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(101): call_user_func(object(closure), object(illuminate\http\request)) #12 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\routing\router.php(703): illuminate\pipeline\pipeline->then(object(closure)) #13 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\routing\router.php(670): illuminate\routing\router->runroutewithinstack(object(illuminate\routing\route), object(illuminate\http\request)) #14 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\routing\router.php(628): illuminate\routing\router->dispatchtoroute(object(illuminate\http\request)) #15 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\http\kernel.php(214): illuminate\routing\router->dispatch(object(illuminate\http\request)) #16 [internal function]: illuminate\foundation\http\kernel->illuminate\foundation\http\{closure}(object(illuminate\http\request)) #17 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(141): call_user_func(object(closure), object(illuminate\http\request)) #18 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\http\middleware\verifycsrftoken.php(43): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #19 c:\xampp\htdocs\proofofconcept\laravel\app\http\middleware\verifycsrftoken.php(17): illuminate\foundation\http\middleware\verifycsrftoken->handle(object(illuminate\http\request), object(closure)) #20 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): app\http\middleware\verifycsrftoken->handle(object(illuminate\http\request), object(closure)) #21 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\view\middleware\shareerrorsfromsession.php(55): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #22 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): illuminate\view\middleware\shareerrorsfromsession->handle(object(illuminate\http\request), object(closure)) #23 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\session\middleware\startsession.php(61): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #24 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): illuminate\session\middleware\startsession->handle(object(illuminate\http\request), object(closure)) #25 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\cookie\middleware\addqueuedcookiestoresponse.php(36): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #26 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): illuminate\cookie\middleware\addqueuedcookiestoresponse->handle(object(illuminate\http\request), object(closure)) #27 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\cookie\middleware\encryptcookies.php(40): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #28 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): illuminate\cookie\middleware\encryptcookies->handle(object(illuminate\http\request), object(closure)) #29 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\http\middleware\checkformaintenancemode.php(42): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #30 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(125): illuminate\foundation\http\middleware\checkformaintenancemode->handle(object(illuminate\http\request), object(closure)) #31 [internal function]: illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #32 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\pipeline\pipeline.php(101): call_user_func(object(closure), object(illuminate\http\request)) #33 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\http\kernel.php(115): illuminate\pipeline\pipeline->then(object(closure)) #34 c:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\illuminate\foundation\http\kernel.php(84): illuminate\foundation\http\kernel->sendrequestthroughrouter(object(illuminate\http\request)) #35 c:\xampp\htdocs\proofofconcept\laravel\public\index.php(53): illuminate\foundation\http\kernel->handle(object(illuminate\http\request)) #36 {main}
edit: moved endpoint upload different controller , it's working fine now, complaining input::file('doctorsnote') nonobject:
[2015-05-15 11:35:20] local.error: exception 'symfony\component\debug\exception\fatalerrorexception' message 'call member function move() on non-object' in c:\xampp\htdocs\proofofconcept\laravel\app\http\controllers\calendarcontroller.php:27 stack trace: #0 {main}
edit: have form it's not registering when click submit button, post not being executed
<form id="logform" enctype="multipart/form-data" action="calendar" method="post"> <select id='logoption'> <option value="0"> select logtype </option> <?php //sql in laravel style log types database $logtypes = db::table('time_log_types')->get(); foreach ($logtypes $logtype) { echo ' <option value="'. $logtype->id . '">' . $logtype->logtype.'</option>'; } ?> </select> <div id="illness" style="display:none"> <p>end date: <input type="text" id="enddate"></p> <input type="hidden" name="max_file_size" value="30000" /> <p>doctor's note: <input type="file" name="doctorsnote" id="doctorsnote"/></p> </div> <input type="button" value="submit" id="submitbutton" style="display:none" /> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> </form>
i managed upload file :d how went it:
step 1: edited form this: - added enctype, added action form, added method post - added hidden field csrftoken - removed javascript related submitting form (don't need ajax call)
<form id="logform" enctype="multipart/form-data" action="calendar" method="post"> <select id='logoption'> <option value="0"> select logtype </option> <?php //sql in laravel style log types database $logtypes = db::table('time_log_types')->get(); foreach ($logtypes $logtype) { echo ' <option value="'. $logtype->id . '">' . $logtype->logtype.'</option>'; } ?> </select> <div id="illness" style="display:none"> <p>end date: <input type="text" name="enddate" id="enddate"></p> <input type="hidden" name="max_file_size" value="30000" /> <p>doctor's note: <input type="file" name="doctorsnote" id="doctorsnote"/></p> </div> <input type="button" value="submit" id="submitbutton" style="display:none" onclick="submit()" /> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> </form>
step 2: remove javascript
step 3: moved upload function calendarcontroller , changed route to:
route::post('calendar', 'calendarcontroller@upload');
i didn't change else (except remove whole javascript function)
and works charm, took me entire day work guess not bad complete novice :d
Comments
Post a Comment