DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 The image “http://spearbang.awardspace.com/avatar.php” cannot be displayed, because it contains errors. From this code <?php header("content-type: image/gif"); $avatars = array( "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Sonic-animated.gif", "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Tails-salutes.gif", "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Sonic-and-Shadow-transform.gif", "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Knuckles2.gif"); echo $avatars[rand(0,3)]; ?> I think i am missing one or two headers in here? Quote Link to comment https://forums.phpfreaks.com/topic/132231-solved-header-problem/ Share on other sites More sharing options...
corbin Posted November 11, 2008 Share Posted November 11, 2008 I think you're missing the entire concept.... Your script is basically returning: HTTP/1.1 200 OK Content-Type: image/gif <some url> Would you really except a browser to display that? A url is not image/gif; it's text. You must read the data out of the files. Eg: header("Content-Type: image/gif"); readfile("someimage.gif"); Quote Link to comment https://forums.phpfreaks.com/topic/132231-solved-header-problem/#findComment-687382 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Author Share Posted November 11, 2008 Ahh, thanks, i have only used headers like this once using image gd, never with reading from files like so. Quote Link to comment https://forums.phpfreaks.com/topic/132231-solved-header-problem/#findComment-687385 Share on other sites More sharing options...
premiso Posted November 11, 2008 Share Posted November 11, 2008 You would need to www.php.net/readfile on the url and echo that out to the site. Here is an example from the php site: <?php $avatars = array( "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Sonic-animated.gif", "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Tails-salutes.gif", "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Sonic-and-Shadow-transform.gif", "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Knuckles2.gif"); $file = $avatars[rand(0,3)]; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } ?> That should work. Quote Link to comment https://forums.phpfreaks.com/topic/132231-solved-header-problem/#findComment-687387 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Author Share Posted November 11, 2008 My code is just <?php header("content-type: image/gif"); $avatars = array( "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Sonic-animated.gif", "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Tails-salutes.gif", "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Sonic-and-Shadow-transform.gif", "http://www.avatarist.com/avatars/Games/Sonic-The-Hedgehog/Knuckles2.gif"); echo readfile($avatars[rand(0,3)]); ?> Just an extra question without wasting forum space, i need the site to use .htaccess to make the site address http://spearbang.awardspace.com/avatar.gif instead of it being http://spearbang.awardspace.com/avatar.php How can i do this? Will this do RewriteEngine On RewriteBase / RewriteRule ^avatar.gif avatar.php [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/132231-solved-header-problem/#findComment-687392 Share on other sites More sharing options...
premiso Posted November 11, 2008 Share Posted November 11, 2008 mod_rewrite would allow for it, but if you do that I would make sure that you add a folder such as: http://site.com/avatars/imageava.gif Then you just parse the part after the avatars folder and use that for your random image. If you do it without doing that all images would be suspect unless you just make it look for the avatars.gif. Either way it would be using mod_rewrite, it has been a while since I used it but I am sure there are a bunch of examples of mod_rewrite online. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/132231-solved-header-problem/#findComment-687397 Share on other sites More sharing options...
corbin Posted November 11, 2008 Share Posted November 11, 2008 Alias avatar.gif avatar.php Or, RewriteEngine On RewriteRule avatar\.gif avatar.php By the way, if the avatars are local, you should be using the file path, not URI. If the file path is not local, why not either download it and save some bandwidth, or just send redirect headers? Quote Link to comment https://forums.phpfreaks.com/topic/132231-solved-header-problem/#findComment-687398 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Author Share Posted November 11, 2008 Solved, and yes i am planning on downloading them just wanted to get the code working. Quote Link to comment https://forums.phpfreaks.com/topic/132231-solved-header-problem/#findComment-687400 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.