0

On my website, I have a form which allows the user to modify a selected item. As a part of this modification, they are allowed the upload (or delete) images associated with the item.

It seems that very specific images are causing my form to not post (I make this assumption because the loading indicator for my browser is pending).

From further inspection, although it does create a file, it's size is 0B. This is concurrent with the fact that the output of $_FILE['tree-photos']['size'][0] is 0. It seems that the image isn't even being attempted to be uploaded as a specific error code is not given by $_FILE['tree-photos']['error'][0] (returns 0).

I have changed all the appropriate file permissions required and the upload_max_size and post_max_size values accordingly. Interestingly, other files from the same folder which follow the exact same naming scheme and are larger upload fine. It seems random which photos trigger the form to not submit, but it is consistent what images do and don't submit.

File extensions are not the problem either, they are consistient.

Here is my code (I have been told off before for not posting all my code so sorry if a lot of it is not required):

<?php
    include("../content/head.php");
    include("../functions.php");
    
    if (!isset($_SESSION['admin'])) {
        header("Location: ../admin/admin.php?page=login");
        exit();
    }

    $id = $_REQUEST['treeID'];

    $tree_sql = "SELECT * FROM trees WHERE treeID=" . $id;
    $tree_query = mysqli_query($dbconnect, $tree_sql);
    $tree_rs = mysqli_fetch_assoc($tree_query);

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

        $com_name = test_input($_POST['com_name']);
        $sci_name = test_input($_POST['sci_name']);
        $height = test_input($_POST['height']);
        $origin = test_input($_POST['origin']);
        $description = test_input($_POST['description']);
        $type = test_input($_POST['type']);

        if(isset($_FILES['tree-photos']['name'][0])) {
            if (!empty($_FILES['tree-photos']['name'][0])) {
                for ($i = 0; $i < count($_FILES['tree-photos']['name']); $i++) {
                    $location = '../images/' . $sci_name .'/' . $sci_name . "_" . uniqid() . "." . strtolower(pathinfo($_FILES['tree-photos']['name'][$i], PATHINFO_EXTENSION));      
                    move_uploaded_file($_FILES['tree-photos']['tmp_name'][$i], $location);
                }
            }
        }

        if ($tree_rs["photo"] == "noimage") {
            $updatesql = "UPDATE trees SET photo='$sci_name' WHERE treeID=".$id;
        }

        $_SESSION['err'] = $_FILES['tree-photos']['error'][0];

        $updatesql = "UPDATE trees SET com_name='$com_name', sci_name='$sci_name', height='$height', origin='$origin', description='$description', type='$type' WHERE treeID=".$id;

        $updatequery = mysqli_query($dbconnect, $updatesql);
    }

    $tree_sql = "SELECT * FROM trees WHERE treeID=" . $id;
    $tree_query = mysqli_query($dbconnect, $tree_sql);
    $tree_rs = mysqli_fetch_assoc($tree_query);

    include("../content/navigation.php");

?>

<div id="main-container">
    <a href="../admin/edittree.php">Back</a>
    <h1><?php echo $_SESSION['err']; ?></h1>
    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])."?treeID=".$id;?>" method="post" enctype="multipart/form-data">
        <p>Common Name</p>
        <input name="com_name" type="text" value="<?php echo $tree_rs['com_name']; ?>">
        <p>Scientific Name</p>
        <input name="sci_name" type="text" value="<?php echo $tree_rs['sci_name']; ?>">
        <p>Height Name</p>
        <input name="height" type="number" value="<?php echo $tree_rs['height']; ?>">
        <p>Origin</p>
        <input name="origin" type="text" value="<?php echo $tree_rs['origin']; ?>">
        <p>Type</p>
        <select name="type">
            <option value="Deciduous">Deciduous</option>
            <option value="Evergreen">Evergreen</option>
        </select>
        <p>Description</p>
        <textarea name="description"><?php echo $tree_rs['description']; ?></textarea>
        <p>Add Photos</p>
        <input name="tree-photos[]" type="file" multiple>
        <?php $tree_rs['photo']; ?>
            <?php
                if ($tree_rs['photo'] != "noimage") { ?>
                    <div class="edit-images-container"> <?php
                    $path = "../images/".$tree_rs['photo']."/";
                    $images = glob("$path*.{jpg,jpeg,png,gif,bmp}", GLOB_BRACE);

                    foreach($images as $image) { ?>
                        <div class="image-editable-container">
                            <img class="editable-image" data-source="<?php echo $tree_rs['sci_name']; ?>" data-id="<?php echo $tree_rs["treeID"]; ?>" src="<?php echo $image?>" alt="<?php echo $tree_rs['com_name'] . " - " . $tree_rs['description']; ?>">
                            <img class="editable-image-delete" src="../images/delete.svg" alt="Delete Button">
                        </div>
                    <?php } ?> </div> <?php
                } else { ?>
                    <p>No Images Currently</p>
                <?php }
            ?>
        <input type="submit" value="Submit">
    </form>
</div>

<?php include("../content/footer.php"); ?

Edit: Included some images that work and don't work for me, would be interesting if its the same for you.

Working image. Not working image.

Edit 2: It seems that after restarting the server I am able to upload around 15 items, including ones I couldn't before. After that, though new random images are un-uploadable. This might be a config issue.

Alex Stiles
  • 151
  • 1
  • 13

0 Answers0