$_FILES[$image_fieldname]['error'] == 0 or handle_error("the server couldn't upload the image you selected.", $php_errors[$_FILES[$image_fieldname]['error']]);
    // Is this file the result of a valid upload?
    @is_uploaded_file($_FILES[$image_fieldname]['tmp_name']) or handle_error("you were trying to do something naughty. Shame on you!", "Uploaded request: file named '{$_FILES[$image_fieldname]['tmp_name']}'");
    // Is this actually an image?
    @getimagesize($_FILES[$image_fieldname]['tmp_name']) or handle_error("you selected a file for your picture that isn't an image.", "{$_FILES[$image_fieldname]['tmp_name']} isn't a valid image file.");
    // Name the file uniquely
    $now = time();
    while (file_exists($upload_filename = $upload_dir . $now . '-' . $_FILES[$image_fieldname]['name'])) {
        //$userpic = $_FILES[$image_fieldname]['name'];
        $now++;
    }
    // Finally, move the file to its permanent location
    @move_uploaded_file($_FILES[$image_fieldname]['tmp_name'], $upload_filename) or handle_error("we had a problem saving your image to its permanent location.", "permissions or related error moving file to {$upload_filename}");
    $original_filename = str_replace($upload_dir, '/', $upload_filename);
    createlargepic($originals_dir, $large_dir, $original_filename, $large_width);
    createthumbs($originals_dir, $thumb_dir, $original_filename, $thumb_width);
    $message = "Congratulations! You successfully uploaded an image and turned it into a thumbnail!";
}
?>
<!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>PHP File upload and create thumnnail test</title>
        </head>
        <body>
            <h1>PHP File upload and create thumnnail test</h1>
            <form name="fileupload" method="post" action="<?php 
echo $_SERVER['PHP_SELF'];
?>
" enctype="multipart/form-data">
<?php

/* code to copy image file form originals folder uploaded to,
then reduce size to thumbnail,
then save in thumbnail directory */
function createthumbs($origImagePath, $tnImagePath, $fname, $thumbWidth)
{
    // 1. open the originals directory
    $dir = opendir($origImagePath);
    // 2. Find the original imaeg file
    //$fname = "1397841364-KamalProfPic1.jpg";
    echo "Creating thumbnail for {$origImagePath}{$fname} <br />";
    // 3. load image and get image size
    $img = imagecreatefromjpeg("{$origImagePath}{$fname}");
    $width = imagesx($img);
    $height = imagesy($img);
    // 4. calculate thumbnail size
    $new_width = $thumbWidth;
    $new_height = floor($height * ($thumbWidth / $width));
    // 5. create a new temporary image
    $tmp_img = imagecreatetruecolor($new_width, $new_height);
    // 6. copy and resize old image into new image
    imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    // 7. save thumbnail into a file
    imagejpeg($tmp_img, "{$tnImagePath}{$fname}");
    // 8. close the directory
    closedir($dir);
}
createthumbs("assets/images/uploads/profpic/originals/", "assets/images/uploads/profpic/thumb/", "1397841364-KamalProfPic1.jpg", 50);