I have a form that allows the user to upload a file, along with some other data. This is then passed to a PHP file which attempts to upload the file, and then write some of the passed data as well as the file location to a JSON index. However, over half the images I attempt to upload will simply print 

> 
> "Sorry, there was an error uploading your file."
> 
> 
> 

which is my catch-all error. The strange part is that some images will display every error code simultaneously, including a PHP error 

> 
> "Undefined index: img\_upload".
> 
> 
> 

Some checking with echo statements showed that in these cases it appears that the image data isn't even being passed to the PHP function. However, when these errors occur the upload ends up succeeding.

PHP:

```
<?php

#Image code
$target_dir = "../BlogPosts/post_images/";
$target_file = $target_dir . basename($_FILES["img_upload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if (isset($_POST["submit"])) {
    $check = getimagesize($_FILES["img_upload"]["tmp_name"]);
    if ($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// // Check file size
//if ($_FILES["img_upload"]["size"] > 500000) {
//    echo "Sorry, your file is too large.";
//    $uploadOk = 0;
//}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["img_upload"]["tmp_name"], $target_file)) {
        echo "The file " . basename($_FILES["img_upload"]["name"]) . " has been uploaded.";

#All other code
        $postbody = $_POST["newpost_body"];
        $posttitle = $_POST["newpost_title"];
#Preparing the post summary
        $postsummary = substr($postbody, 0, 175);
        $postsummary = $postsummary . "...";
        $postsummary = strip_tags($postsummary);
        $postsummary = preg_replace("/&quot;/", "\"", $postsummary);
        $postsummary = preg_replace("/&#39;/", "'", $postsummary);

#Uploading post text
        $filepath = dirname(__FILE__) . "/../BlogPosts/";
        $fileid = time();
        $filename = $filepath . $fileid . ".html";
        $var_str = var_export($postbody, true);
        $var = "$postbody";
        file_put_contents($filename, $var);
        $posts = json_decode(file_get_contents(dirname(__FILE__) . "/../BlogPosts/posts.json"), true);
#$posts->append($fileid);
        $filedata = [
            "ID" => $fileid,
            "Title" => $posttitle,
            "Date" => gmdate("m.d.y", $fileid),
            "Summary" => $postsummary,
            "Thumb" => $target_file,
        ];

        array_push($posts['posts'], $filedata);

        $arr1 = $posts;
        file_put_contents(dirname(__FILE__) . "/../BlogPosts/posts.json", json_encode($arr1));
# array.json => {"a":1,"b":2,"c":3,"d":4,"e":5}

        header('Location: #/blog');
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

```

HTML Form:

```
<form id="bpost_form" class="dropzone" method="post" action="/admin/writetofile.php" style="text-align:center" enctype="multipart/form-data">
            <input type="text" name="newpost_title"/>
            <br>
            <input type="file" name="img_upload" id="fileToUpload">
            <br>
            <textarea name="newpost_body" id="newpost" rows="10" cols="80">
                This is my textarea to be replaced with CKEditor.
            </textarea>
            <script>
                // Replace the <textarea id="editor1"> with a CKEditor
                // instance, using default configuration.
                CKEDITOR.replace('newpost');
            </script>
            <input type="submit"
                   value="Save"/>
        </form>

```