Problems with upload form for pictures

https://www.zaaltjeoudenbos.rf.gd
I have a form where users can upload an image to a folder uploads. But when I try uploading files larger than 3Mb, I receive a 502 Bad Gateway: nginx error.
The code I used to upload my picture is placed below. When I upload a smaller image (300Kb) it is uploaded. Is there someone who experienced something similar?

<?php
   require_once('./connection.php');

// Uit te voeren wanneer het formulier wordt verzonden
if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $category_id = $_POST["category_id"];
   $description = $_POST["description"];

   // Controleren of er een foto is geupload
   if (!empty($_FILES["image"]["name"])) {
       $target_dir = "uploads/";
       $target_file = $target_dir . basename($_FILES["image"]["name"]);
       $uploadOk = 1;
       $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

       // Bestandextentie controleren
       $check = getimagesize($_FILES["image"]["tmp_name"]);
       if($check !== false) {
           $uploadOk = 1;
       } else {
           echo "Het geupload bestand is geen afbeelding.";
           $uploadOk = 0;
       }

       // Contoleren of afbeeldingen niet dubbel worden geupload
       if (file_exists($target_file)) {
           echo "Het bestand is reeds geupload.";
           $uploadOk = 0;
       }

       // Groote van afbeelding controleren (10 000 000 bytes = 10 Mb)
       if ($_FILES["image"]["size"] > 10000000) {
           echo "Het bestand is te groot.";
           $uploadOk = 0;
       }

       // Controle op bestandstype
       if($imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "jpeg"
       && $imageFileType != "gif" ) {
           echo "Enkel bestanden van type: jpg, png of jpeg.";
           $uploadOk = 0;
       }

       // Als de upload niet voltooid is:
       if ($uploadOk == 0) {
           echo $uploadOk;
           echo "Het bestand is niet geupload.";
       // Bestand uploaden als alles ok is
       } else {
           echo '<script>console.log("ok"); </script>';
           if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
               // Pad in de databank opslaan
               $sql = "INSERT INTO photos (category_id, path, description) VALUES ('$category_id', '$target_file', '$description')";
               if (mysqli_query($conn, $sql)) {
                   echo "De foto is met succes toegevoegd aan de foto gallerij";
               } else {
                   echo "Error: " . $sql . "<br>" . mysqli_error($conn);
               }
           } else {
               echo "Er is een fout ontstaan tijdens het uploaden van uw foto.";
           }
       }
   }
}

Not sure if a limit for image files exists, but here’s the article I found.

PHP image upload script not working - Informal - InfinityFree Forum

4 Likes

I while back did some experiments uploading images with PHP on my website but didn’t have any issues and could upload images up to the server limit of 10MB

2 Likes

If your having issues, I would reccomend uploading images to an external provider, such as S3.

3 Likes

My guess is that your script is being killed, most likely due to hitting memory limits enforced by the kernel or taking too long.

But images larger than 3MB are not for the website anyway
(I don’t know for what purpose they should be bigger unless it’s a 4K+ wallpaper)

Are you sure that the problem is related to 3MB+ or it is simply something from my first sentence ?

2 Likes

Storj is free for your first 25GB, and you can selfhost it.

2 Likes

Thanks to everyone that responded.
I fixed it by adding these two lines to my htaccess folder.

php_value upload_max_filesize 10M
php_value post_max_size 10M

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.