1 (edited by citanul 2012-11-12 11:14)

Topic: Having to refresh to see new avatars (+dirty hack/fix)

It's because it always uses the same filenames for avatars (same filenames after changes, so the browser just uses the image with the same name from its cache). My possible solution would be to hack the code to always make a random filename, delete the old one and reassign the new avatar URL to the new filename.

I attempted to create a sort of fix... it basically adds a "timestamp" in the form of C-style time in between the user ID and extension, and well... see for yourselves. It's certainly not the nicest solution, and I'm rather novice to PHP, but it works for me. Apparently. Except that the old image deleting thing isn't working so well, and if you re-upload something it seems to somehow use the old upload.

profile.php diff:

1195a1196,1199
>                             // Set up so-called ctime thing
>                             // 11 digits (a few zeros followed by Unix time?)
>                             $ctime = sprintf('.%\'011u',time());
> 
1197c1201
<                             @rename($avatar_tmp_file, $forum_config['o_avatars_dir'].'/'.$id.$extension);
---
>                             @rename($avatar_tmp_file, $forum_config['o_avatars_dir'].'/'.$id.$ctime.$extension);

include/functions.php diff:

40c40
<     if ($return != null)
---
>     if ($return != null) 
543c543
<     $avatar_markup = $avatar_filename = '';
---
>     $avatar_markup = $avatar_filename = $avatar_ext = '';
549,550c549
< 
<     // Create avatar filename
---
>     // Create avatar extension
554c553
<             $avatar_filename = $user_id.'.gif';
---
>             $avatar_ext = '.gif';
558c557
<             $avatar_filename = $user_id.'.jpg';
---
>             $avatar_ext = '.jpg';
562c561
<             $avatar_filename = $user_id.'.png';
---
>             $avatar_ext = '.png';
571c570
<     if ($avatar_filename && $avatar_width > 0 && $avatar_height > 0)
---
>     if ($avatar_ext && $avatar_width > 0 && $avatar_height > 0)
573c572,578
<         $path = $forum_config['o_avatars_dir'].'/'.$avatar_filename;
---
>         $glob_stuff = FORUM_ROOT.$forum_config['o_avatars_dir'].'/'.$user_id.'.'.'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'.$avatar_ext;
>         $avatars = glob($glob_stuff);
>         if ($avatars == null)
>             return;
> 
>         //$path = $forum_config['o_avatars_dir'].'/'.$avatar_filename;
>         $path = $avatars[0];
2044c2049
<     $filetypes = array('jpg', 'gif', 'png');
---
>     $filetypes = array('.jpg', '.gif', '.png');
2053,2054c2058,2067
<         $avatar = FORUM_ROOT.$forum_config['o_avatars_dir'].'/'.$user_id.'.'.$cur_type;
<         if (file_exists($avatar))
---
>         $glob_stuff = FORUM_ROOT.$forum_config['o_avatars_dir'].'/'.$user_id.'.'.'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'.$cur_type;
>         $avatars = glob($glob_stuff);
> 
>         if ($avatars == null && $cur_type != '.png')
>             next;
>         elseif ($avatars == null && $cur_type == '.png')
>             return;
> 
> 
>         foreach ($avatars as $avatar)
2056c2069,2072
<             @unlink($avatar);
---
>             if (file_exists($avatar))
>             {
>                 @unlink($avatar);
>             }