Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:11:56+00:00 2026-05-28T04:11:56+00:00

I have a script which uploads an image to the server under the id

  • 0

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?

Script:

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

    // check if larger than 5mb
    $avatar = $_FILES['uploadedAvatar'];
    if( $avatar["size"] > 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 '<span style="background-color:#E05641;" class="pint2">'.$error.'</span>';
    ##### 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 < $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->query($sql);
        ##### END PROCESS THUMBNAIL AND UPLOAD PICTURE ##### 

        echo '<span class="pint2">'.SETTINGSphoto_chage_success.' <a style="color:#ffffff" href="'.$_ENV['rootURL'].'/'.( in_array($_GET['lang'],$_ENV['supportedLanguages']) ? $_GET['lang']."/" : $nothing).HEADlanguage_exchange.'/id/'.$memberID.'">'.SETTINGSphoto_view_profile.'</a></span>';
        }
}    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:11:57+00:00Added an answer on May 28, 2026 at 4:11 am

    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?

    You can solve this by telling the user she should clear the browser cache.

    If that is not an option, you want to actually prevent the user to cache the image.

    So you need to make a difference between each revision of the image. Add a integer field to your database you store the revision number of the image. Then each time you output the user’s image, fetch the actual revision number from the database as well. Add it as the query-info-part of the image URL:

    <img src="avatars/user/929292/photo.jpg?revision">
                                            ^^^^^^^^
    

    The browser will still cache profile pictures but you make him aware of the revision, so a specific revision is cached.

    Old avatar picture:

    <img src="avatars/user/929292/photo.jpg?1">
    

    Then the user uploads a new image, you increment the revision field, the new avatar image is:

    <img src="avatars/user/929292/photo.jpg?2">
    

    If the revision changes, the browser notices that and will pick the image from the server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script which, each time is called, gets the first line of
I have a php-script, which uploads mp3-files on my server. I use 'uploadify' for
I have a script which logs on to a remote server and tries to
I have a script which creates a user-defined object like this: obj = new
the procedure: i have a script which uploads images (via hmtl-form and php) for
I have a php script which uploads images to a temporary folder on the
I have a ajax image upload script which i found here http://www.fengcool.com/2009/06/ajax-form-upload-local-image-file-without-refresh/ The problem
I have a small php script which allows me to upload image file on
i have one image upload script which upload same image on two locations same
I currently have a script which will pull in image contents from a database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.