php-gallery/upload.php

101 lines
3.5 KiB
PHP
Raw Normal View History

2022-07-21 14:53:04 +00:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2022-07-25 15:13:26 +00:00
<title>Upload</title>
2022-07-21 14:53:04 +00:00
<link rel="stylesheet" href="css/master.css">
<link href="https://fonts.googleapis.com/css2?family=Rubik" rel="stylesheet">
2022-07-23 14:03:11 +00:00
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Lexend+Deca:wght@600&amp;display=swap">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@500&amp;display=swap">
2022-07-21 14:53:04 +00:00
</head>
<body>
<?php
2022-08-02 13:13:35 +00:00
include "ui/required.php";
2022-07-25 15:13:26 +00:00
include("ui/header.php");
2022-07-21 14:53:04 +00:00
2022-07-26 20:51:59 +00:00
// Check if user is logged in
2022-08-01 09:31:23 +00:00
if (loggedin()) {
2022-07-26 20:51:59 +00:00
// User is logged in
} else {
$error = "You must be logged in to upload images";
header("Location: index.php");
}
// Setting up varibles
$dir = "images/";
$thumb_dir = $dir."thumbnails/";
$image_basename = basename($_FILES["image"]["name"]);
$image_path = $dir.$image_basename;
$file_type = pathinfo($image_path,PATHINFO_EXTENSION);
// Continue if no errors
if (isset($_POST['upload']) && !empty($_FILES["image"]["name"])) {
if (empty($error)) {
$allowed_types = array('jpg', 'jpeg', 'png', 'webp');
if (in_array($file_type, $allowed_types)) {
// Upload to server
if (move_uploaded_file($_FILES['image']['tmp_name'], $image_path)) {
// Make thumbnail
$image_thumbnail = new Imagick($image_path);
$image_thumbnail->resizeImage(300,null,null,1,null);
$image_thumbnail->writeImage($thumb_dir.$image_basename);
2022-07-21 14:53:04 +00:00
2022-07-26 11:33:28 +00:00
// Prepare sql for destruction and filtering the sus
$sql = "INSERT INTO swag_table (imagename, alt, author) VALUES (?, ?, ?)";
2022-07-21 14:53:04 +00:00
2022-07-26 11:33:28 +00:00
if ($stmt = mysqli_prepare($conn, $sql)) {
// Bind the smelly smelly
mysqli_stmt_bind_param($stmt, "sss", $param_image_name, $param_alt_text, $param_user_id);
2022-07-21 14:53:04 +00:00
2022-07-26 11:33:28 +00:00
// Setting up parameters
2022-07-26 20:51:59 +00:00
$param_image_name = $_FILES["image"]["name"];
2022-07-26 11:33:28 +00:00
$param_alt_text = $_POST['alt'];
$param_user_id = $_SESSION["id"];
2022-07-26 11:33:28 +00:00
// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
2022-07-26 20:51:59 +00:00
$success = "Your Image uploaded successfully!";
2022-07-26 11:33:28 +00:00
} else {
$error = "Something went fuckywucky, please try later";
}
}
2022-07-26 20:51:59 +00:00
} else {
$error = "F, Upload failed";
2022-07-25 17:28:55 +00:00
}
2022-07-23 07:44:43 +00:00
} else {
2022-07-26 20:51:59 +00:00
$error = "File uploaded not supported, file types that are allowed include: JPG, JPEG, PNG and WEBP";
2022-07-21 14:53:04 +00:00
}
}
}
?>
2022-08-01 13:09:53 +00:00
<div class="alert-banner">
<?php
if (isset($error)) {
echo notify($error, "low");
}
if (isset($success)) {
2022-08-01 14:06:27 +00:00
echo notify($success, "high");
2022-08-01 13:09:53 +00:00
}
?>
<script src='scripts/alert.js'></script>
</div>
2022-07-26 17:16:17 +00:00
<div class="upload-root default-window">
2022-07-25 15:13:26 +00:00
<h2 class="space-bottom">Upload image</h2>
<p>In this world you have 2 choices, to upload a really cute picture of an animal or fursuit, or something other than those 2 things.</p>
<form class="flex-down between" method="POST" action="upload.php" enctype="multipart/form-data">
<input class="btn alert-default space-bottom" type="file" name="image" placeholder="select image UwU">
<input class="btn alert-default space-bottom-large" type="text" name="alt" placeholder="Description/Alt for image">
2022-07-26 17:16:17 +00:00
<button class="btn alert-high" type="submit" name="upload"><img class="svg" src="assets/icons/upload.svg">Upload Image</button>
2022-07-25 15:13:26 +00:00
</form>
</div>
2022-07-26 12:34:48 +00:00
<?php
include("ui/top.html");
include("ui/footer.php");
2022-07-26 12:34:48 +00:00
?>
2022-07-21 14:53:04 +00:00
</body>
</html>