-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg_upload.php
55 lines (45 loc) · 1.75 KB
/
img_upload.php
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
<?php
include("session_check.php");
if ($_SESSION['role'] == 2) {
$RUserID = $_SESSION['userid'];
// file name
$filename = $_FILES['file']['name'];
// Location
$dir = "uploads/" . $RUserID . "/";
$location = $dir . $filename;
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
// file extension
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
$file_extension = strtolower($file_extension);
// Valid image extensions
$image_ext = array("jpg", "png", "jpeg");
$response = 0;
if (in_array($file_extension, $image_ext)) {
if (file_exists($location)) unlink($location);
// Upload file
if (move_uploaded_file($_FILES['file']['tmp_name'], $location)) {
$response = $location;
$foodid = $_POST['foodid'];
$newname = $dir . $foodid . "." . $file_extension;
rename($location, $newname);
include("conn.php");
$exist = "SELECT FoodID FROM Food WHERE FoodID = $foodid";
$result = mysqli_query($con, $exist);
$count = mysqli_num_rows($result);
if ($count == 0) {
$imagedir = $con->prepare("INSERT INTO Food (FoodID, FoodImage, RUserID) VALUES (?, ?, ?)");
$imagedir->bind_param("sss", $foodid, $newname, $_SESSION['userid']);
$imagedir->execute();
} else {
$imagedir = $con->prepare("UPDATE Food SET FoodImage = ? WHERE RUserID = ? AND FoodID = ?");
$imagedir->bind_param("sss", $newname, $_SESSION['userid'], $foodid);
$imagedir->execute();
}
}
}
echo $response;
} else {
echo 0;
}