├── .gitignore ├── README.md ├── _form.php ├── create.php ├── delete.php ├── index.php ├── partials ├── footer.php ├── header.php └── not_found.php ├── update.php ├── users ├── users.json └── users.php └── view.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | users/images/* 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHPSimpleCRUD-FS 2 | Simple PHP CRUD application with data saved in JSON file 3 | 4 | ## Features 5 | 6 | - Read users JSON file and display data in bootstrap table 7 | - Implement create and update forms for a user 8 | - Implement delete user functionality 9 | - Add image uploading functionality to every user 10 | - Implement form validation and do not submit form on invalid data 11 | 12 | The project was created while recording [youtube video](https://youtu.be/DWHZSkn5paQ) 13 | -------------------------------------------------------------------------------- /_form.php: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |

5 | 6 | Update user 7 | 8 | Create new User 9 | 10 |

11 |
12 |
13 | 14 |
16 |
17 | 18 | 20 |
21 | 22 |
23 |
24 |
25 | 26 | 28 |
29 | 30 |
31 |
32 |
33 | 34 | 36 |
37 | 38 |
39 |
40 |
41 | 42 | 44 |
45 | 46 |
47 |
48 |
49 | 50 | 52 |
53 | 54 |
55 |
56 |
57 | 58 | 59 |
60 | 61 | 62 |
63 |
64 |
65 |
66 | -------------------------------------------------------------------------------- /create.php: -------------------------------------------------------------------------------- 1 | '', 8 | 'name' => '', 9 | 'username' => '', 10 | 'email' => '', 11 | 'phone' => '', 12 | 'website' => '', 13 | ]; 14 | 15 | $errors = [ 16 | 'name' => "", 17 | 'username' => "", 18 | 'email' => "", 19 | 'phone' => "", 20 | 'website' => "", 21 | ]; 22 | $isValid = true; 23 | 24 | if ($_SERVER['REQUEST_METHOD'] === 'POST') { 25 | $user = array_merge($user, $_POST); 26 | 27 | $isValid = validateUser($user, $errors); 28 | 29 | if ($isValid) { 30 | $user = createUser($_POST); 31 | 32 | uploadImage($_FILES['picture'], $user); 33 | 34 | header("Location: index.php"); 35 | } 36 | } 37 | 38 | ?> 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /delete.php: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 |
11 |

12 | Create new User 13 |

14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 35 | 36 | 37 | 38 | 39 | 44 | 53 | 54 | 55 | 56 |
ImageNameUsernameEmailPhoneWebsiteActions
31 | 32 | " alt=""> 33 | 34 | 40 | 41 | 42 | 43 | 45 | View 46 | Update 48 |
49 | 50 | 51 |
52 |
57 |
58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /partials/footer.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /partials/header.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | Simple PHP CRUD 13 | 14 | 15 | -------------------------------------------------------------------------------- /partials/not_found.php: -------------------------------------------------------------------------------- 1 |
2 |

User does not exist

3 |
4 | -------------------------------------------------------------------------------- /update.php: -------------------------------------------------------------------------------- 1 | "", 19 | 'username' => "", 20 | 'email' => "", 21 | 'phone' => "", 22 | 'website' => "", 23 | ]; 24 | 25 | if ($_SERVER['REQUEST_METHOD'] === 'POST') { 26 | $user = array_merge($user, $_POST); 27 | 28 | $isValid = validateUser($user, $errors); 29 | 30 | if ($isValid) { 31 | $user = updateUser($_POST, $userId); 32 | uploadImage($_FILES['picture'], $user); 33 | header("Location: index.php"); 34 | } 35 | } 36 | 37 | ?> 38 | 39 | 40 | -------------------------------------------------------------------------------- /users/users.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 4, 4 | "name": "Patricia Lebsack", 5 | "username": "Karianne", 6 | "email": "Julianne.OConner@kory.org", 7 | "phone": "123451234", 8 | "website": "kale.biz", 9 | "extension": "jpg" 10 | }, 11 | { 12 | "id": 5, 13 | "name": "Chelsey Dietrich", 14 | "username": "Kamren", 15 | "email": "Lucio_Hettinger@annie.ca", 16 | "phone": "453218563", 17 | "website": "demarco.info", 18 | "extension": "jpg" 19 | }, 20 | { 21 | "id": 6, 22 | "name": "Mrs. Dennis Schulist", 23 | "username": "Leopoldo_Corkery", 24 | "email": "Karley_Dach@jasper.info", 25 | "phone": "1078412367", 26 | "website": "ola.org", 27 | "extension": "jpg" 28 | }, 29 | { 30 | "id": 7, 31 | "name": "Kurtis Weissnat", 32 | "username": "Elwyn.Skiles", 33 | "email": "Telly.Hoeger@billy.biz", 34 | "phone": "210.067.6132", 35 | "website": "elvis.io" 36 | }, 37 | { 38 | "id": 8, 39 | "name": "Nicholas Runolfsdottir V", 40 | "username": "Maxime_Nienow", 41 | "email": "Sherwood@rosamond.me", 42 | "phone": "586.493.6943 x140", 43 | "website": "jacynthe.com" 44 | }, 45 | { 46 | "id": 9, 47 | "name": "Glenna Reichert", 48 | "username": "Delphine", 49 | "email": "Chaim_McDermott@dana.io", 50 | "phone": "(775)976-6794 x41206", 51 | "website": "conrad.com" 52 | }, 53 | { 54 | "id": 10, 55 | "name": "Clementina DuBuque", 56 | "username": "Moriah.Stanton", 57 | "email": "Rey.Padberg@karina.biz", 58 | "phone": "024-648-3804", 59 | "website": "ambrose.net" 60 | }, 61 | { 62 | "name": "John Doe", 63 | "username": "john22", 64 | "email": "", 65 | "phone": "12345", 66 | "website": "", 67 | "id": 1879571, 68 | "extension": "jpg" 69 | } 70 | ] -------------------------------------------------------------------------------- /users/users.php: -------------------------------------------------------------------------------- 1 | $user) { 42 | if ($user['id'] == $id) { 43 | $users[$i] = $updateUser = array_merge($user, $data); 44 | } 45 | } 46 | 47 | putJson($users); 48 | 49 | return $updateUser; 50 | } 51 | 52 | function deleteUser($id) 53 | { 54 | $users = getUsers(); 55 | 56 | foreach ($users as $i => $user) { 57 | if ($user['id'] == $id) { 58 | array_splice($users, $i, 1); 59 | } 60 | } 61 | 62 | putJson($users); 63 | } 64 | 65 | function uploadImage($file, $user) 66 | { 67 | if (isset($_FILES['picture']) && $_FILES['picture']['name']) { 68 | if (!is_dir(__DIR__ . "/images")) { 69 | mkdir(__DIR__ . "/images"); 70 | } 71 | // Get the file extension from the filename 72 | $fileName = $file['name']; 73 | // Search for the dot in the filename 74 | $dotPosition = strpos($fileName, '.'); 75 | // Take the substring from the dot position till the end of the string 76 | $extension = substr($fileName, $dotPosition + 1); 77 | 78 | move_uploaded_file($file['tmp_name'], __DIR__ . "/images/${user['id']}.$extension"); 79 | 80 | $user['extension'] = $extension; 81 | updateUser($user, $user['id']); 82 | } 83 | } 84 | 85 | function putJson($users) 86 | { 87 | file_put_contents(__DIR__ . '/users.json', json_encode($users, JSON_PRETTY_PRINT)); 88 | } 89 | 90 | function validateUser($user, &$errors) 91 | { 92 | $isValid = true; 93 | // Start of validation 94 | if (!$user['name']) { 95 | $isValid = false; 96 | $errors['name'] = 'Name is mandatory'; 97 | } 98 | if (!$user['username'] || strlen($user['username']) < 6 || strlen($user['username']) > 16) { 99 | $isValid = false; 100 | $errors['username'] = 'Username is required and it must be more than 6 and less then 16 character'; 101 | } 102 | if ($user['email'] && !filter_var($user['email'], FILTER_VALIDATE_EMAIL)) { 103 | $isValid = false; 104 | $errors['email'] = 'This must be a valid email address'; 105 | } 106 | 107 | if (!filter_var($user['phone'], FILTER_VALIDATE_INT)) { 108 | $isValid = false; 109 | $errors['phone'] = 'This must be a valid phone number'; 110 | } 111 | // End Of validation 112 | 113 | return $isValid; 114 | } 115 | -------------------------------------------------------------------------------- /view.php: -------------------------------------------------------------------------------- 1 | 18 |
19 |
20 |
21 |

View User:

22 |
23 |
24 | Update 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 | 55 | 56 | 57 |
Name:
Username:
Email:
Phone:
Website: 51 | 52 | 53 | 54 |
58 |
59 |
60 | 61 | 62 | 63 | --------------------------------------------------------------------------------