PHP to crop uploaded image and then upload original and cropped version
<p>My objective is to (1) upload one image, (2) crop that image into a thumbnail, and (3) upload both images to a folder and into my database table. The reason is to avoid uploading two separate images.</p>

<p>The code uploads the uncropped image but not the cropped "thumbnail." (The cropping function is in another file.) My tests show that the thumbnail version exists because it displays. Both files have the names I want and both insert into the database. </p>

<p>The cropped thumbnail however does not upload.  I am stuck. If anyone can review this code and find any errors, I would appreciate it.</p>

<pre><code>&lt;!-- - - - - - - - - - - - - Insert new image  - - - - - - - - - - - - - --&gt;
&lt;?php
if(isset($_POST['action']) &amp;&amp; $_POST['action'] === 'add_new_gallery_image'){

    // Store posted data into new variables
    $image = $_FILES['image']['name'];
    $image_tmp = $_FILES['image']['tmp_name'];
    $title = sanitize($_POST['title']);
    $alt = sanitize($_POST['alt']);

    // Connect to database
    include 'includes/dbconnect.php';

    // Check if fields have input  
    if(!isset($title) || $title === '' || !isset($alt) || $alt === ''){

        $errMsg = '*Please fill in all fields before submitting.';
        include 'includes/error.html.php';
        exit();  
    }
    else {

        // Move uploaded file to assigned folder (here "uploaded_gallery_images") http://php.net/manual/en/function.move-uploaded-file.php
        move_uploaded_file($image_tmp, "../uploaded_gallery_images/$image");

        // Remove file extension from image file
        $image = pathinfo($image, PATHINFO_FILENAME);

        // Set up name for thumbnail
        $thumbnail = $image  . '_thumb.jpg';

        // Call function to create thumbnail (parameter 1 is path to newly uploaded image, parameter 2 is extension, parameter 3 is dimension (square). Resource: http://www.thewebhelp.com/php/functions/create-square-thumbs/
        create_square_image("../uploaded_gallery_images/$image.jpg", $thumbnail, 100);


    /*
        // Test to demonstrate images retrieved
        echo "&lt;img src=../uploaded_gallery_images/$image.jpg&gt;";  // path reflects image already moved to folder
        echo "&lt;img src=$thumbnail&gt;";  // cropped image not moved to folder yet, so it displays with this path
        exit();
    */

        // Move uploaded file to assigned folder (here "uploaded_gallery_images") http://php.net/manual/en/function.move-uploaded-file.php
        move_uploaded_file($thumbnail, "../uploaded_gallery_images/$thumbnail");

        include 'includes/dbconnect.php';
        $table = 'gallery';

        // Re-attach extension before insertion into database table
        $image = $image . '.jpg';

        try {
            $sql = "INSERT INTO $table SET
            image = :image,
            thumbnail = :thumbnail,
            title = :title,
            alt = :alt";

            $s = $db-&gt;prepare($sql);
            $s-&gt;bindValue(':image', $image);
            $s-&gt;bindValue(':thumbnail', $thumbnail);
            $s-&gt;bindValue(':title', $title);
            $s-&gt;bindValue(':alt', $alt);
            if( $s-&gt;execute() ){
                echo "&lt;script&gt;alert('Image added!')&lt;/script&gt;";
                echo "&lt;script&gt;window.location.href = 'index.php'&lt;/script&gt;";                              
            } 
        }
        catch (PDOException $e) {
            $errMsg = 'Error inserting data into database: ' . $e-&gt;getMessage();
            include 'includes/error.html.php';
            exit();
        }

        // Close database connection
        $db = null;

        //header('Location: .');
        exit();
    }                                      
} 
</code></pre>