-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
64 lines (62 loc) · 2.2 KB
/
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
56
57
58
59
60
61
62
63
64
<?php
require_once("require/database.php");
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 0;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Kollar om det är ett image eller bara ett fake!
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
} if ($_FILES["fileToUpload"]["size"] > 1000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
} if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
} else {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
} else {
echo "File is not an image.";
$uploadOk = 0;
}
if ($uploadOk == 0 ) {
echo "Your file was not uploaded!";
sleep(3);
header("location: index.php");
$_SESSION["img_error"] = 1;
} else {
$desc = mysqli_real_escape_string($conn ,$_POST["desc"]);
$desc_fix = trim($desc);
$desc_inp = filter_input(INPUT_POST, $desc_fix, FILTER_SANITIZE_SPECIAL_CHARS);
if (strlen($desc) > 1000) {
$_SESSION["descerr"] = 1;
} else {
$filename = tempnam('uploads/', 'img');
unlink($filename);
$period_position = strrpos($filename, ".");
$filename = substr($filename, 0, $period_position);
$file = substr($filename, -7);
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $filename)) {
echo "The file has been uploaded.";
$useruploading = $_SESSION["user_id"];
$date = date("Y-m-d");
$sql = "INSERT INTO imgs (images, date, description, userid) VALUES ('$file', '$date', '$desc_input', '$useruploading')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
sleep(5);
$_SESSION['success'] = $uploadOk;
header("location: index.php");
} else {
echo "Sorry, there was an error uploading your file.";
$_SESSION["img_error"] = 1;
}
}
}
?>