Skip to content

Commit fa230f7

Browse files
feat: file uploads
1 parent 1701f63 commit fa230f7

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Php Rest API Application

controllers/file.controller.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/* File Controller Class */
4+
class FileController {
5+
6+
function __construct() { }
7+
8+
/* Check Method Function */
9+
function fileController($method) {
10+
switch ($method) {
11+
12+
/* POST Method */
13+
case "POST":
14+
15+
/* Image Allowed Extension */
16+
$allowed_extension = array("png", "jpg","jpeg, webp");
17+
18+
/* Get Image File Extension */
19+
$file_extension = pathinfo($_FILES["upload"]["name"], PATHINFO_EXTENSION);
20+
21+
/* Check Extension */
22+
if(!in_array(strtolower($file_extension),$allowed_extension)){
23+
echo json_encode(array('status'=>'Fail', 'error'=>'Please upload png, jpg, jpeg and webp file.'));
24+
die();
25+
}
26+
27+
/* Check File Size */
28+
if($_FILES["upload"]["size"] > 1024*1024){
29+
echo json_encode(array('status'=>'Fail', 'error'=>'Please upload 1MB size file.'));
30+
die();
31+
}
32+
33+
/* Upload File Path */
34+
$url ="uploads/IMG_".uniqid()."_".date("GHisdmY").".".$file_extension;
35+
36+
/* Upload File */
37+
if(move_uploaded_file($_FILES['upload']['tmp_name'], $url)){
38+
39+
/* Check HTTPS */
40+
if(isset($_SERVER['HTTPS'])){
41+
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
42+
}
43+
else{ $protocol = 'http'; }
44+
45+
/* Upload File Link */
46+
$url = $protocol."://".$_SERVER['SERVER_NAME'] ."/php-rest-api/".$url;
47+
48+
echo json_encode(array('status'=>'Success', 'message'=>'File is successful uploaded.', 'file_url' => $url));
49+
50+
}
51+
break;
52+
53+
/* DELETE Method */
54+
case "DELETE":
55+
56+
/* Recive Delete File URL */
57+
$data = json_decode(file_get_contents('php://input'), true);
58+
59+
/* Check HTTPS */
60+
if(isset($_SERVER['HTTPS'])){
61+
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
62+
}
63+
else{ $protocol = 'http'; }
64+
65+
/* Remove Host Link in URL */
66+
$url = str_replace($protocol."://".$_SERVER['SERVER_NAME'] ."/php-rest-api/", "",$data['upload']);
67+
68+
/* Delete File */
69+
if(unlink($url)){
70+
echo json_encode(array('status'=>'Success', 'message'=>'File is deleted.'));
71+
}
72+
break;
73+
74+
default:
75+
echo json_encode(array('status'=>'Fail', 'error'=>'Please use POST and DELETE Method for file.'));
76+
77+
}
78+
}
79+
}
80+
81+
?>

controllers/method.controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ class MethodController {
55

66
function __construct() { }
77

8-
/* Check Method Class */
9-
function MethodController($method, $table, $id) {
8+
/* Check Method Function */
9+
function methodController($method, $table, $id) {
1010
switch ($method) {
1111

1212
/* GET Method */
@@ -45,7 +45,7 @@ function MethodController($method, $table, $id) {
4545
/* DELETE Method */
4646
case "DELETE":
4747

48-
/* Include PUT Model File */
48+
/* Include DELETE Model File */
4949
include 'models/delete.model.php' ;
5050
$delete = new DeleteModel();
5151
$delete->deleteData($table, $id);

index.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
if(!isset($_GET['id'])) { $id = null; }
99
else { $id = $_GET['id']; }
1010

11+
/* Check Upload File */
12+
if(isset($_GET['table']) && $_GET['table'] === "file") {
13+
include 'controllers/file.controller.php';
14+
$file = new FileController();
15+
$file->FileController($_SERVER['REQUEST_METHOD']);
16+
die();
17+
}
18+
1119
/* Method Controller File */
1220
include 'controllers/method.controller.php';
1321
$method = new MethodController();
14-
$method->MethodController($_SERVER['REQUEST_METHOD'], $table , $id);
22+
$method->methodController($_SERVER['REQUEST_METHOD'], $table , $id);
1523

1624
?>

uploads/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)