51 lines
1.2 KiB
Markdown
51 lines
1.2 KiB
Markdown
|
|
![[Pasted image 20241117113802.png]]
|
||
|
|
|
||
|
|
```php
|
||
|
|
$studentObject = new StudentModel();
|
||
|
|
|
||
|
|
$imageFile = $this->request->getFile("profile_image");
|
||
|
|
|
||
|
|
// only go through image storing logic if image actually exists in payload
|
||
|
|
if (isset($imageFile) && !empty($imageFile)) { // with image
|
||
|
|
|
||
|
|
$imageName = $imageFile->getName();
|
||
|
|
$tempArray = explode(".", $imageName);
|
||
|
|
$newImageName = round(microtime(true)) . "" . end($tempArray);
|
||
|
|
|
||
|
|
if($imageFile->move("images/" . $newImageName)) { // move automatically creates images folder if not exists
|
||
|
|
$data = [
|
||
|
|
"name" => $this->request->getVar("name"),
|
||
|
|
"email" => $this->request->getVar("email"),
|
||
|
|
"profile_image" => "images/" . $newImageName
|
||
|
|
];
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
$response = [
|
||
|
|
"status" => false,
|
||
|
|
"message" => "Image Upload failed",
|
||
|
|
"data" => []
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else { // without image
|
||
|
|
$data = [
|
||
|
|
"name" => $this->request->getVar("name"),
|
||
|
|
"email" => $this->request->getVar("email"),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
// store student
|
||
|
|
if ($studentObject->insert($data)) {
|
||
|
|
$response = [
|
||
|
|
"status" => true,
|
||
|
|
"message" => "Student saved successfully",
|
||
|
|
"data" => []
|
||
|
|
];
|
||
|
|
} else {
|
||
|
|
$response = [
|
||
|
|
"status" => false,
|
||
|
|
"message" => "Failed to save student",
|
||
|
|
"data" => []
|
||
|
|
];
|
||
|
|
}
|
||
|
|
```
|