-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadPicture.php
79 lines (68 loc) · 2.02 KB
/
uploadPicture.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
if(empty($_SESSION['id'])){
header('location:login.html');
exit;
}
if(isset($_FILES["fileToUpload"]) && !empty($_FILES["fileToUpload"]['name'])){
$target_dir = "uploads/user$id_User/";
echo "yooo";
if (!is_dir($target_dir)) {
mkdir($target_dir, 0777, true);
}
$basename = basename($_FILES["fileToUpload"]["name"]);
$imageFileType = strtolower(pathinfo($basename,PATHINFO_EXTENSION));
$target_file = $target_dir."post".$lastid.".".$imageFileType;
$uploadOk = 1;
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
if (!file_exists($_FILES['fileToUpload']['tmp_name'])) {
echo "File upload failed. ";
if (isset($_FILES['fileToUpload']['error'])) {
echo "Error code: ".$_FILES['fileToUpload']['error'];
}
exit;
}
$file_temp = $_FILES['fileToUpload']['tmp_name'];
echo $file_temp;
$check = getimagesize($file_temp);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "file exists";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "file is too big";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "file not good";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
// if everything is ok, try to upload file
} else {
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
}
elseif(empty($description)) {
header('location:index.php');
exit;
}
if(!empty($target_file)){
$req = mysqli_prepare($db, "UPDATE objectposts SET Url_Media = ? WHERE ID = ?");
mysqli_stmt_bind_param($req, "si", $target_file, $lastid);
mysqli_stmt_execute($req);
}
?>