refresh image on upload
<p>I have a script which uploads an image to the server under the id of the user. To change teh picture the first gets overwritten. The problem is that the old picture, with the dimensions of the new one, shows for a while until they refresh their browser. How can I prevent this from happening?</p>

<p>Script:</p>

<pre><code>function uploadProfilePicture($memberID,$extension)
{
    ##### FIND ERRORS #####
    $error = "";

    // check if larger than 5mb
    $avatar = $_FILES['uploadedAvatar'];
    if( $avatar["size"] &gt; 5000000 ){ //5mb
        $error = SETTINGSphoto_less_5meg;
    }
    //check if member selected a photo
    if( $avatar['name'] == "" ){
        $error = SETTINGSphoto_no_upload;
    }

    //check if photo is of allowed formats
    if(  $avatar["type"] != "image/png"  ){
      if(  $avatar["type"] != "image/gif"  ){
        if(  $avatar["type"] != "image/jpg"  ){
            if(  $avatar["type"] != "image/jpeg"  ){
                if(  $avatar["type"] != "image/pjpeg"  ){
                    $error = SETTINGSphoto_file_type;   
                }
            }
         }
      }
   }

    #### END FIND ERRORS #####      

    ##### DISPLAYS ERRORS ######
    if( $error != ""){
            echo '&lt;span style="background-color:#E05641;" class="pint2"&gt;'.$error.'&lt;/span&gt;';
    ##### END DISPLAYS ERRORS ######


    ##### PROCESS THUMBNAIL AND UPLOAD PICTURE #####
    }else{



        $ext = substr($avatar["name"], -4); //take off extention. 
        $fileName = $memberID.$extension.$ext; // rename picture with member's ID.w
        unlink("photos/".$memberID.$extension.".jpg");
        unlink("photos/".$memberID.$extension."Thumb.jpg");

        copy($avatar['tmp_name'], "photos/".$fileName); //upload temp

        //create virual image
        if(preg_match('/[.](jpg)$/',strtolower($avatar["name"]))) {  
            $im = imagecreatefromjpeg("photos/".$fileName);  
        } else if (preg_match('/[.](gif)$/', strtolower($avatar["name"]))) {  
            $im = imagecreatefromgif("photos/".$fileName);  
        } else if (preg_match('/[.](png)$/', strtolower($avatar["name"]))) {  
            $im = imagecreatefrompng("photos/".$fileName);  
        } else if (preg_match('/[.](jpeg)$/',strtolower($avatar["name"]))) {  
            $im = imagecreatefromjpeg("photos/".$fileName); 
        }else{
            $im = imagecreatefromjpeg("photos/".$fileName); 
        }

        //get height and width
        $ox = imagesx($im);  $oy = imagesy($im);  

        $final_width_of_image = 200;
        $final_height_of_image = 200;
        //$ratio = $final_width_of_image / $ox; //find ratio to apply to height
        //$nx = $final_width_of_image; $ny = $oy * $ratio; 
        if( $ox &lt; $oy ){

                $nx = 200; 
                $ny = floor($oy * (200 / $ox)); 

        }else{

                $ny = 200; 
                $nx = floor($ox * (200 / $oy)); 

        }


        //$nm = imagecreatetruecolor($nx, $ny);   
        //imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);    //create new pic    
        $nm = imagecreatetruecolor($nx, $ny);   
        imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);  

        unlink("photos/".$fileName); //delete temp image

        imagejpeg($nm, "photos/".$memberID.$extension.".jpg",100); //save image // 100 = quality 

        $im = imagecreatefromjpeg("photos/".$memberID.$extension.".jpg");  
        $nm = imagecreatetruecolor(200, 200);
        imagecopyresampled($nm, $im,0,0,0,0,200,200,200,200);
        imagejpeg($nm, "photos/".$memberID.$extension."Thumb.jpg",100);



        $sql = "UPDATE exchange SET photo = 1 WHERE id = '$memberID'"; //update db
        $mysql = new mysql();
        $mysql-&gt;query($sql);
        ##### END PROCESS THUMBNAIL AND UPLOAD PICTURE ##### 

        echo '&lt;span class="pint2"&gt;'.SETTINGSphoto_chage_success.' &lt;a style="color:#ffffff" href="'.$_ENV['rootURL'].'/'.( in_array($_GET['lang'],$_ENV['supportedLanguages']) ? $_GET['lang']."/" : $nothing).HEADlanguage_exchange.'/id/'.$memberID.'"&gt;'.SETTINGSphoto_view_profile.'&lt;/a&gt;&lt;/span&gt;';
        }
}    
</code></pre>
