Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
1.8 kB
2
Indexable
Never
if (isset($_FILES['uploaded_image'])) {
            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["uploaded_image"]["name"]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

            // Check if image file is an actual image or fake image
            $check = getimagesize($_FILES["uploaded_image"]["tmp_name"]);
            if ($check !== false) {
                $uploadOk = 1;
            } else {
                $error = "File is not an image.";
                $uploadOk = 0;
            }

            // Check file size (limit set to 5MB)
            if ($_FILES["uploaded_image"]["size"] > 5000000) {
                $error = "Sorry, your file is too large.";
                $uploadOk = 0;
            }

            // Allow certain file formats
            if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
                && $imageFileType != "gif") {
                $error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                $uploadOk = 0;
            }

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                $error = "Sorry, your file was not uploaded.";
            // If everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["uploaded_image"]["tmp_name"], $target_file)) {
                    $uploadedImageURL = $target_file;
                    $error = "The file " . htmlspecialchars(basename($_FILES["uploaded_image"]["name"])) . " has been uploaded.";
                } else {
                    $error = "Sorry, there was an error uploading your file.";
                }
            }
        }
    }
Leave a Comment