Jump to content

[SOLVED] Header Problem


DeanWhitehouse

Recommended Posts

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?

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.