forked from afgprogrammer/PHP-MVC-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHome.php
More file actions
77 lines (58 loc) · 2.05 KB
/
Home.php
File metadata and controls
77 lines (58 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
use MVC\Controller;
class ControllersHome extends Controller {
public function index() {
// Connect to database
$model = $this->model('home');
// Read All Task
$users = $model->getAllUser();
// Prepare Data
$data = ['data' => $users];
// Send Response
$this->response->sendStatus(200);
$this->response->setContent($data);
}
public function post() {
if ($this->request->getMethod() == "POST") {
// Connect to database
$model = $this->model('home');
// Read All Task
$users = $model->getAllUser();
// Prepare Data
$data = ['data' => $users];
// Send Response
$this->response->sendStatus(200);
$this->response->setContent($data);
}
}
public function uploadImage() {
if(isset($this->request->files['image'])){
$image = $this->request->files['image'];
$errors = array();
// File info
$file_name = $image['name'];
$file_size = $image['size'];
$file_tmp = $image['tmp_name'];
$file_type = $image['type'];
// Get file extension
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
// White list extensions
$extensions = array("jpeg","jpg","png");
// Check it's valid file for upload
if(in_array($file_ext, $extensions) === false) {
$errors[] = "Extension not allowed, please choose a JPEG or PNG file.";
}
// Check file size
if($file_size > 2097152) {
$errors[] = 'File size must be exactly 2 MB';
}
if(empty($errors) == true) {
move_uploaded_file($file_tmp, UPLOAD . "Images/" . $file_name);
echo "Success";
} else {
print_r($errors);
}
}
}
}