【发布时间】:2016-11-15 08:41:44
【问题描述】:
大家好!在我的 Laravel 应用程序中,我有一个 Excel 文件的上传功能。我从网上获得了这段代码,并将其调整为我的应用程序。问题是,它没有捕捉到用户提交文件但没有选择文件时产生的致命错误异常。我不明白为什么它没有被抓住。我将添加我的控制器的一部分。
public function upload() {
$file = array('thefile' => Input::file('thefile'));
$rules = array('excel' => 'excel');
$validator = Validator::make($file, $rules);
if ($validator->fails()) {
return Redirect::to('UploadExcelFile')->withInput()->withErrors($validator);
}
else {
try{
// FatalErrorException happens in this line!
if (Input::file('thefile')->isValid()) {
$destinationPath = 'uploads';
$fileName = Input::file('thefile')->getClientOriginalName();
Input::file('thefile')->move($destinationPath, $fileName);
Session::flash('success', 'Upload successfully');
$fileNameJSON = exec("python /path/to/script/ExcelToJSON3.py $fileName"); // This returns a full path name of the JSON file that is made...
if ($fileNameJSON == null){
return Redirect::to('/dashboard/input');
}
else {
// Getting the ID from the file that is uploaded
try {
$jsonDecode = json_decode(file_get_contents($fileNameJSON));
} catch (ErrorException $e){
return Redirect::to('/errorpage')->with(array('status'=> 'ErrorException'));
}
A lot of code for handling the data entered....
}catch (FatalErrorException $e){
return Redirect::to('/errorpage')->with(array('status'=> 'FatalErrorException'));
}
}
return true;
}
给出的错误:
UploadExcelFileController.php 第 35 行中的 FatalErrorException: 在非对象上调用成员函数 isValid()
所以,我不明白为什么这段代码不能处理错误异常以及如何解决这个问题!
【问题讨论】:
-
您是否将类顶部的
Symfony\Component\Debug\Exception\FatalErrorException导入为use Symfony\Component\Debug\Exception\FatalErrorException? -
是的!我已经导入了带有完整路径名的 FatalErrorException,没有完整路径名,就像@Andy 建议的那样。但我仍然得到同样的错误..