-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_file.upload.php
More file actions
51 lines (44 loc) · 1.5 KB
/
06_file.upload.php
File metadata and controls
51 lines (44 loc) · 1.5 KB
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
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_FILES["fileToUpload"])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if image file is actual
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false) {
echo "✅ File is an image - " . $check["mime"] . "<br>";
} else {
echo "❌ Not an image.<br>";
$uploadOk = 0;
}
// Check if file exists
if (file_exists($target_file)) {
echo "❌ File already exists.<br>";
$uploadOk = 0;
}
// Limit file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "❌ File too large.<br>";
$uploadOk = 0;
}
// Allow only images
$allowedTypes = ["jpg", "jpeg", "png", "gif"];
if (!in_array($imageFileType, $allowedTypes)) {
echo "❌ Only JPG, JPEG, PNG & GIF allowed.<br>";
$uploadOk = 0;
}
// Try to upload
if ($uploadOk == 1) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "✅ The file <strong>" . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . "</strong> has been uploaded.";
} else {
echo "❌ Error uploading file.";
}
} else {
echo "❌ File was not uploaded.";
}
} else {
echo "⚠️ Please upload via the form.";
}
?>