你好我在laravel 4.2中创建一个项目,在特定文件夹中上传不同的文件(例如Pdf和图像),大约8或9个文件夹,我只是尝试做一个if语句,但不知道发生了什么,但它不起作用,执行代码时没有显示错误。请帮我处理这段代码,或者你有任何想法简化我的代码TIA!
这是我控制器中的代码
public function upload()
{
$finame = Input::get('name');
$ftype = Input::get('type');
$username = Input::get('user');
$desc = Input::get('desc');
$date = Input::get('date');
$upload = Input::file('file');
$add = Files::upload2($finame,$ftype,$username,$desc,$date,$upload);
if ($add == true && $ftype == "pf") {
$upload -> move(public_path('pf'),$upload->getClientOriginalName());
return \Response::json(array('success' => true));
return Redirect::to('/home')->with('message', 'message|Record Successfully Added.');
} elseif ($add == true && $ftype == "sf") {
$upload -> move(public_path('sf'),$upload->getClientOriginalName());
return \Response::json(array('success' => true));
return Redirect::to('/home')->with('message', 'message|Record Successfully Added.');
} } elseif ($add == true && $ftype == "gale") {
$upload -> move(public_path('gale'),$upload->getClientOriginalName());
return \Response::json(array('success' => true));
return Redirect::to('/home')->with('message', 'message|Record Successfully Added.');
} } elseif ($add == true && $ftype == "advisory") {
$upload -> move(public_path('advisory'),$upload->getClientOriginalName());
return \Response::json(array('success' => true));
return Redirect::to('/home')->with('message', 'message|Record Successfully Added.');
} else {
return Redirect::back()->with('message', 'error|Error');
}
}
我在模型中的代码:
public static function upload2($finame,$ftype,$username,$desc,$date,$upload)
{
$files = new self;
$files->file_name = $finame;
$files->file_type = $ftype;
$files->username = $username;
$files->description = $desc;
$files->date = $date;
$files->upload = $upload->getClientOriginalName();
try {
$files->save();
} catch (Exception $e) {
dd($e);
}
}
并在视图中添加此脚本:
<script>
var form = document.getElementById('upload');
var request = new XMLHttpRequest();
form.addEventListener('submit', function(e){
e.preventDefault();
var formdata = new FormData(form);
request.open('post', '/upload');
request.addEventListener("load", transferComplete);
request.send(formdata);
});
function transferComplete(data){
response = JSON.parse(data.currentTarget.response);
if(response.success){
document.getElementById('message').innerHTML = "File Successfully Uploaded!";
}
}
</script>