Repository navigation
Expand file tree
/
Copy pathupload.php
More file actions
61 lines (54 loc) · 1.97 KB
/
Copy pathupload.php
File metadata and controls
61 lines (54 loc) · 1.97 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
52
53
54
55
56
57
58
59
60
61
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "root", "", "babylow");
// Check if the "images" folder exists, if not create it
if (!file_exists("images")) {
mkdir("images", 0777, true);
}
// Check if form has been submitted
if(isset($_POST['submit'])){
// Get file information from $_FILES array
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
// Get the file extension
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
// List of allowed file extensions
$allowed = array('jpg', 'jpeg', 'png', 'gif');
// Check if the file extension is allowed
if (in_array($fileActualExt, $allowed)) {
// Check for any errors during the upload
if ($fileError === 0) {
// Check if the file size is within limits
if ($fileSize < 1000000) {
// Generate a unique file name
$fileNameNew = uniqid('', true).".".$fileActualExt;
// Define the destination folder and file name
$fileDestination = 'images/'.$fileNameNew;
// Move the uploaded file to the destination folder
move_uploaded_file($fileTmpName, $fileDestination);
// Insert image path into the database
$sql = "INSERT INTO images (path) VALUES ('$fileDestination')";
mysqli_query($conn, $sql);
// Redirect to index page with success message
header("Location: index.php?uploadsuccess");
} else {
echo "Your file is too big!";
}
} else {
echo "There was an error uploading your file!";
}
} else {
echo "You cannot upload files of this type!";
}
}
?>
<!-- HTML form to select and upload a file -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit">Upload</button>
</form>