• PHP Image header issue
    10 replies, posted
On my site, to avoid avatars being cached I'm trying to get the PHP to display the users avatar based off an id. This works fine, the issue I'm having is that when the file can't be found it should show another image, however it isn't. It will show an avatar if it finds one, if it doesn't then it instead just shows the file location and name, rather than the default avatar. [php]<?php include ("./mysql.php"); // open the file in a binary mode $userid = $_GET['id']; $get_user_avatar_sql="SELECT * FROM users WHERE id='$userid'"; $get_user_avatar_query=mysql_query($get_user_avatar_sql); $get_user_avatar_array=mysql_fetch_array($get_user_avatar_query); $get_user_avatar=$get_user_avatar_array['avatar']; $get_user_avatar = "." . $get_user_avatar; if ($get_user_avatar != '') { $avatar_fp = fopen($get_user_avatar, 'rb'); // send the right headers header("Content-Type: image/png"); header("Content-Length: " . filesize($get_user_avatar)); // dump the picture and stop the script fpassthru($avatar_fp); } else { $default_image = "../avatar/default.png"; $default_image = fopen($default_image, 'rb'); // send the right headers header("Content-Type: image/png"); header("Content-Length: " . filesize($default_image)); // dump the picture and stop the script fpassthru($default_image); } exit; ?> [/php] I know all the directories and such are correct so it should be able to find and display the file.
[php] $get_user_avatar = "." . $get_user_avatar; if ($get_user_avatar != '') { [/php] Take a good look. Also see the PHP function: [url=http://php.net/manual/en/function.file-exists.php]file_exists()[/url]
Ah thanks, I did try file_exists before but still had the same problem, will try again now knowing why that wasn't working. [editline]03:27PM[/editline] Works great now, thanks. [php]<?php include ("./mysql.php"); function unknownAvatar () { $default_image = "../avatar/default.png"; $default_image = fopen($default_image, 'rb'); //send the right headers header("Content-Type: image/png"); //header("Content-Length: " . filesize($default_image)); //dump the picture and stop the script fpassthru($default_image); } // open the file in a binary mode $userid = $_GET['id']; $get_user_avatar_sql="SELECT * FROM users WHERE id='$userid'"; $get_user_avatar_query=mysql_query($get_user_avatar_sql); $get_user_avatar_array=mysql_fetch_array($get_user_avatar_query); $get_user_avatar=$get_user_avatar_array['avatar']; $get_user_avatar = "." . $get_user_avatar; if ($get_user_avatar != ".") { if (file_exists($get_user_avatar)) { $avatar_fp = fopen($get_user_avatar, 'rb'); // send the right headers header("Content-Type: image/png"); //header("Content-Length: " . filesize($get_user_avatar)); // dump the picture and stop the script fpassthru($avatar_fp); } else { unknownAvatar (); } } else { unknownAvatar(); } exit; ?> [/php]
[QUOTE=loony383;19799722]On my site, to avoid avatars being cached[/QUOTE] Why would you want that?
[QUOTE=turby;19817020]Why would you want that?[/QUOTE] When someone views someone's profile or a post, it would cache their avatar, then if that user changes their avatar, due to it using the same filename and overwriting old avatars, it would not update for people that have it cached.
So you do what HTTP wants you to do and wait for the cache to expire. I would prefer to see an out-of-date avatar for a few hours than to have to reload every single avatar each page load. Quicker for me, and less load on your server
[url]http://www.facepunch.com/image.php?u=100245&dateline=1255287695[/url] Facepunch does the same, therefore your complaining about facepunch. Each avatar is limited to 20 KB on my site, it's hardly anything to worry about.
[QUOTE=loony383;19820408][url]http://www.facepunch.com/image.php?u=100245&dateline=1255287695[/url] Facepunch does the same, therefore your complaining about facepunch.[/QUOTE] No, facepunch allows the browser to cache the image. [img]http://imgkk.com/i/JFh8_r.pngp[/img]
Ah, I assume it did as it links to a php file also. for now I'm going to keep it to non cache because I'm still constantly changing the login system, maybe once I've got it solid and don't need to change my avatar often and see the changes I'll make it so it can cache.
[QUOTE=loony383;19831020]Ah, I assume it did as it links to a php file also. for now I'm going to keep it to non cache because I'm still constantly changing the login system, maybe once I've got it solid and don't need to change my avatar often and see the changes I'll make it so it can cache.[/QUOTE] ??
[QUOTE=loony383;19820408][url]http://www.facepunch.com/image.php?u=100245&dateline=1255287695[/url] Facepunch does the same, therefore your complaining about facepunch. Each avatar is limited to 20 KB on my site, it's hardly anything to worry about.[/QUOTE] No it doesn't vBulletin uses &dateline= on the image.php to show you the latest version of their avatar.
Sorry, you need to Log In to post a reply to this thread.