1
All right, here’s the deal, I have a registration system in Windows, in the edit part, the form loads the fields filled with the photo, if I click update and do not choose an image file again appears the error: "Call to a Member Function getClientOriginalExtension() on null".
CONTROLLER
public function detalhes($id, Request $request){
$data = array();
$data["lista"] = \App\Curso::all();
try{
$alu = \App\Aluno::find($id);
if($request->isMethod("POST")){
$matricula = $request->input("matricula", "");
$nome = $request->input("nome", "");
$telefone = $request->input("telefone", "");
$sexo = $request->input("sexo", "");
$email = $request->input("email", "");
$endereco = $request->input("endereco", "");
$bairro = $request->input("bairro", "");
$cep = $request->input("cep", "");
$cidade = $request->input("cidade", "");
$estado = $request->input("estado", "");
$idcurso = $request->input("idcurso", "");
$file = $request->file("foto");
$ext = $file->getClientOriginalExtension();
$size = $file->getSize();
if($ext != "jpg" && $ext != "png" && $ext != "jpeg"){
$data["resp"] = "<div class='alert alert-info'>"
. "Escolha uma IMAGEM valida</div>";
//2MB
}else if($size > (1024 * 1024 * 2)){
$data["resp"] = "<div class='alert alert-info'>"
. "Tamanho da imagem invalido</div>";
}else{
$fileName = "ft_" .date('YmdHis').".".$ext;
$alu->matricula = $matricula;
$alu->nome = $nome;
$alu->telefone = $telefone;
$alu->sexo = $sexo;
$alu->email = $email;
$alu->idcurso = $idcurso;
$alu->foto = $fileName;
$alu->save();
$idend = $alu->endereco->idendereco;
$e = \App\Endereco::find($idend);
$e->endereco = $endereco;
$e->bairro = $bairro;
$e->cidade = $cidade;
$e->cep = $cep;
$e->estado = $estado;
$e->aluno()->associate($alu);
$e->save();
$file->move("fotos", $fileName);
$data["resp"] = "<div class='alert alert-success'>"
. "Aluno editado com sucesso!</div>";
$alu = \App\Aluno::find($id);
return redirect('admin/buscar.html');
}
}
$data["a"] = $alu;
} catch (Exception $ex) {
$data["resp"] = "<div class='alert alert-danger'>"
. "Operação não realizada</div>";
}
return view('aluno/detalhes', $data);
}
But it already loads the image that was registered, but clicking update gives this error as if the image was not chosen yet. For example, I want to change and update only the form zip code and keep the rest including the photo that was uploaded. It loads the photo but it’s like it hasn’t uploaded. Even the photo showing up I have to choose the same photo again then it sends normal.
– Evandro
@I see. I changed the example code. This time the algorithm will check if the user sent a photo, if it sent it, does the size and extension check; otherwise, ignore this part.
– Valdeir Psr
Thank you very much Valdeir, you saved my day.
– Evandro