Laravel 5上传的zip文件显示未定义的变量:e

时间:2018-07-23 11:52:27

标签: laravel laravel-5

我已经创建了文件上传方法,并且文件扩展名为jpg, png等的文件上传成功。但是,当我上传一个zip文件时,它说我“ Undefined variable: e”。为了找到答案,我在相应方法的开头做了dd(),但它仍然显示上述错误。我的路线如下:

Route::post('media/savedata/{id?}/{page?}', 'Administrator\MedialibraryController@savedata'); 

方法如下:

public function savedata($id, $page, Request $request) {
 ...
}

“网络”标签的内容如下:

Request URL: http://localhost/stmd/administrator/media/savedata/0/1
Request Method: POST
Status Code: 500 Internal Server Error
Remote Address: [::1]:80
Referrer Policy: no-referrer-when-downgrade

------WebKitFormBoundaryaHtERzAAThbQB4ae
Content-Disposition: form-data; name="_token"

TXLvBGRXrdjpQbhAjm18MTxkiHamauvcjhKBhl6n
------WebKitFormBoundaryaHtERzAAThbQB4ae
Content-Disposition: form-data; name="fileType"

I
------WebKitFormBoundaryaHtERzAAThbQB4ae
Content-Disposition: form-data; name="fileName"; filename="gradle-3.5-bin.zip"
Content-Type: application/zip

我的意图是:如果上传zip文件,它将重定向到另一个页面,但是我无法执行此操作,因为即使dd()也无法正常工作。

更新:

下面是Laravel日志:

[2018-07-23 11:59:51] local.ERROR: Undefined variable: e {"exception":"[object] (ErrorException(code: 0): Undefined variable: e at /var/www/html/stmd/app/Exceptions/Handler.php:76)
[stacktrace]
#0 /var/www/html/stmd/app/Exceptions/Handler.php(76): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8, 'Undefined varia...', '/var/www/html/s...', 76, Array)
#1 /var/www/html/stmd/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(83): App\\Exceptions\\Handler->render(Object(Illuminate\\Http\\Request), Object(Illuminate\\Http\\Exceptions\\PostTooLargeException))
#2 /var/www/html/stmd/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(55): Illuminate\\Routing\\Pipeline->handleException(Object(Illuminate\\Http\\Request), Object(Illuminate\\Http\\Exceptions\\PostTooLargeException))
#3 /var/www/html/stmd/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#4 /var/www/html/stmd/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#5 /var/www/html/stmd/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#6 /var/www/html/stmd/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#7 /var/www/html/stmd/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#8 /var/www/html/stmd/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#9 /var/www/html/stmd/public/index.php(58): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#10 /var/www/html/stmd/index.php(4): include('/var/www/html/s...')
#11 {main}
"} 

我的Handler.php内容如下:

public function render($request, Exception $exception)
    {
        if($this->isHttpException($exception)){
            $logFile = 'laravel.log';

            Log::useDailyFiles(storage_path().'/logs/'.$logFile);
            //echo Route::getCurrentRoute()->getAction();



            //echo $exception->getStatusCode();exit;
            switch ($exception->getStatusCode()) {

                case 404:
                    Log::info('Unable to find page.');
                    return response()->view('errors.404',[],404);

                    break;

                case 500:

                    Log::error('In Server is really going wrong.');
                    return response()->view('errors.500',[],500);

                    break;
                 default:
                    return $this->renderHttpException($e);
                break;

            }

        }
        return parent::render($request, $exception);
    }
}

3 个答案:

答案 0 :(得分:2)

更改

return $this->renderHttpException($e);

return $this->renderHttpException($exception);

app/Exceptions/Handler.php

答案 1 :(得分:0)

您可以在app/Exceptions/Handler.php.的异常处理程序中处理FileException异常,更新render方法以添加所需的代码。例如,如果您想返回验证异常,则需要在FileHandler异常的异常处理程序中处理验证。

public function render($request, Exception $exception)
{
    if ($exception instanceof \Symfony\Component\HttpFoundation\File\Exception\FileException) {
        // create a validator and validate to throw a new ValidationException
        return Validator::make($request->all(), [
            'your_file_input' => 'required|file|size:5000',
        ])->validate();
    }

    return parent::render($request, $exception);
}

这是未经测试的,但应该可以让您大致了解。

答案 2 :(得分:0)

if ($exception instanceof \Illuminate\Http\Exceptions\PostTooLargeException)
            return \Redirect::to('administrator/media')->with('errorMessage', 'Please upload an image.');