laravel 中的文件上传

file upload in laravel

我想在 laravel.this 中创建文件上传是 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.[=15= 中上传文件时,我通过 www.mysite.com/send-file 和 "Null" displayed.but 调用发送文件] 并将文件发送到 www.mysite。com/send-file 错误 displayed.in 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 configuration.I 在根目录中放置一个 .htaccess 文件 laravel root 并且这是 .htaccess :

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

    RewriteEngine On

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

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

您的路线需要是 POST 路线:

Route::post('/send-file', function()
{
    var_dump(Input::file('file'));
});

显然文件上传不能通过 GET 进行。此外,使用 Laravels 助手创建的表单的默认方法是 POST.

顺便说一下,MethodNotAllowedHttpException通常表示找到匹配的路由(URL是正确的)但是方法很好...... 不允许。所以要么你的路线有错误的方法,要么你试图用错误的方法访问它。