Php 在laravel中上传文件

Php 在laravel中上传文件,php,apache,.htaccess,laravel,Php,Apache,.htaccess,Laravel,我想在laravel中创建一个文件上载。这是panel.blade.php: {{ Form::open(array('url'=>'send-file','files'=>true)) }} {{ Form::label('file', 'File' ,array()) }} {{ Form::file('file', '', array()) }} {{ Form::submit('send') }} {{ Form::close() }} 这是routes.php: Rou

我想在laravel中创建一个文件上载。这是panel.blade.php:

{{ Form::open(array('url'=>'send-file','files'=>true)) }} 
{{ Form::label('file', 'File' ,array()) }}
{{ Form::file('file', '', array()) }}
{{ Form::submit('send') }}
{{ Form::close() }}
这是routes.php:

Route::get('/panel/', function()
{
    return View::make('panel.index');
});
Route::get('/send-file', function()
{
    var_dump(Input::file('file'));
});
我通过www.mysite.com/send-file调用send file并显示“Null”。但当我想在www.mysite.com/panel中重新加载文件并将文件发送到www.mysite.com/send-file时,会显示错误。在laravel日志错误中,会显示此错误:

[2015-01-07 06:45:18] production.ERROR: exception 'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException' in /var/www/usb/bootstrap/compiled.php:5750
Stack trace:
#0 /var/www/usb/bootstrap/compiled.php(5746): Illuminate\Routing\RouteCollection->methodNotAllowed(Array)
#1 /var/www/usb/bootstrap/compiled.php(5724): Illuminate\Routing\RouteCollection->getOtherMethodsRoute(Object(Illuminate\Http\Request), Array)
#2 /var/www/usb/bootstrap/compiled.php(5048): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
#3 /var/www/usb/bootstrap/compiled.php(5036): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#4 /var/www/usb/bootstrap/compiled.php(5028): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#5 /var/www/usb/bootstrap/compiled.php(715): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#6 /var/www/usb/bootstrap/compiled.php(696): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#7 /var/www/usb/bootstrap/compiled.php(7800): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#8 /var/www/usb/bootstrap/compiled.php(8407): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#9 /var/www/usb/bootstrap/compiled.php(8354): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#10 /var/www/usb/bootstrap/compiled.php(11017): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#11 /var/www/usb/bootstrap/compiled.php(657): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#12 /var/www/usb/index.php(49): Illuminate\Foundation\Application->run()
#13 {main} [] []
我想这是关于httpd配置的错误。我在根laravel根中放了一个.htaccess文件,这是.htaccess:

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

选项-多视图
重新启动发动机
#重定向尾部斜杠。。。
重写规则^(.*)/$/$1[L,R=301]
#处理前控制器。。。
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写规则^index.php[L]

您的路线需要是一条
POST
路线:

Route::post('/send-file', function()
{
    var_dump(Input::file('file'));
});
显然,通过GET上传文件不起作用。另外,使用Laravels助手创建的表单的默认方法是POST


顺便说一句,MethodNotAllowedHttpException通常意味着找到了匹配的路由(URL是正确的),但该方法很好地。。。不允许。因此,要么您的路线使用了错误的方法,要么您试图使用错误的方法访问它。

您的路线需要是
POST
路线:

Route::post('/send-file', function()
{
    var_dump(Input::file('file'));
});
显然,通过GET上传文件不起作用。另外,使用Laravels助手创建的表单的默认方法是POST


顺便说一句,MethodNotAllowedHttpException通常意味着找到了匹配的路由(URL是正确的),但该方法很好地。。。不允许。因此,要么你的路由使用了错误的方法,要么你试图用错误的方法访问它。

该方法不是GET,而是POST方法,因为你正在向PHP发送数据。该方法不是GET,而是POST方法,因为你正在向PHP发送数据。