Php Laravel POST令牌不匹配异常(文件上载)

Php Laravel POST令牌不匹配异常(文件上载),php,jquery,html,ajax,laravel,Php,Jquery,Html,Ajax,Laravel,我有一个功能,它可以创建一个弹出窗口,并对其做出一定的响应。我有一个html代码,它是我的弹出框。它使用jquery对话框来显示弹出窗口,但表单位于我的其他html代码之间,被一个div包围,使其不可见: <form id="logform" method="POST"> <select id='logoption'> <option value="0"> Select logtype </option> <?php //SQL in

我有一个功能,它可以创建一个弹出窗口,并对其做出一定的响应。我有一个html代码,它是我的弹出框。它使用jquery对话框来显示弹出窗口,但表单位于我的其他html代码之间,被一个div包围,使其不可见:

<form id="logform" method="POST"> 
<select id='logoption'>  
<option value="0"> Select logtype </option>
<?php 
//SQL in laravel style to get log types from database
$logtypes = DB::table('time_log_types')->get();

foreach ($logtypes as $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>
最后,这是我在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 the uploads fail due to file system, you 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);
        }
    }
}
我完全不知道发生了什么,我根据来自各地的指南创建了这段代码,据我所知,这应该是可行的,但我对这一切都是新手,我真的需要一些帮助来解决这个问题

编辑:我在表单中添加了一个新字段,以包含csrf令牌,如下所示:

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
编辑:我将上载的端点移动到了另一个控制器,现在工作正常,但现在它抱怨我的输入::文件“doctorsnote”是一个非对象:

[2015-05-15 11:35:20] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function move() on a non-object' in C:\xampp\htdocs\proofofconcept\laravel\app\Http\Controllers\CalendarController.php:27
Stack trace:
#0 {main}  
编辑:我现在有我的表格,但它没有注册,当我点击提交按钮时,帖子没有被执行

<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 to get log types from database
$logtypes = DB::table('time_log_types')->get();    
foreach ($logtypes as $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>

清除浏览器的缓存(包括cookie)。按住Ctrl键并刷新表单页面。请重试提交

编辑: 只需在表单中包含此元素


我刚刚上传了一个文件:D 我是怎么做的:

步骤1:按如下方式编辑我的表单: -添加了enctype,在我的表单中添加了操作,添加了方法post -添加了csrftoken的隐藏字段 -删除了所有与提交我的表单相关的javascript,我不需要这个ajax调用

<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 to get log types from database
$logtypes = DB::table('time_log_types')->get();

foreach ($logtypes as $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>
除了删除整个javascript函数外,我没有更改任何其他内容


它就像一个符咒,花了我一整天的时间才开始工作,但我想对于一个完全的新手来说还不错:D

文档,是你最好的朋友吗?我读了那里的文档,但我真的不知道该怎么处理它,我可能没有提到它,但我是所有这些技术的完全新手,我的公司基本上给了我使用laravel的任务我曾经为学校实习做过一些php错误修复,但就是这样,我自己解决了所有问题,这里没有其他人对我所做的任何事情有任何经验,如果你看到令牌不匹配,这是laravel的CRSF验证,基本上,您必须在请求中包含令牌。在标题上或在表单中添加隐藏输入。再次阅读CRSF令牌部分如果我尝试以下方法:或者{csrf_field}在我的表单中,我的表单所在的页面是一个刀片,然后我的html不再生成,它会给我一个html页面,页面上只有单词“calendar”,我不知何故能够添加csrf令牌,但现在我遇到了一个不同的问题,我已经尝试过了,这并没有解决问题,对不起,我迟到了。我正在和老板一起吃午饭
[2015-05-15 11:21:23] local.ERROR: exception 'ReflectionException' with message 'Class App\Http\Controllers\LogController does 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}  
[2015-05-15 11:35:20] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function move() on a non-object' in C:\xampp\htdocs\proofofconcept\laravel\app\Http\Controllers\CalendarController.php:27
Stack trace:
#0 {main}  
<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 to get log types from database
$logtypes = DB::table('time_log_types')->get();    
foreach ($logtypes as $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>
<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 to get log types from database
$logtypes = DB::table('time_log_types')->get();

foreach ($logtypes as $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>
Route::post('calendar', 'CalendarController@upload');