0

I have 2 websites and would like to reuse pictures from site A within side B using PHP.

I have looked into @readfile() and

header('Content-Type: image/png');
echo(file_get_contents('http://site_a.com/img.png'));

Because I like to show the picture inside my content, I get an obvious headers already sent error.

Is there a way around this or an other function to show a png picture which is stored on my other site?

Ask asked to show the code:

<html>
<head>
<title>test</title>
</head>
<body>
<h1>Content</h1>
<?php 
 header('Content-Type: image/png');
 @readfile('http://site_a.net/img/picture.png');
?>
</body>
</html>
luvTweb
  • 159
  • 1
  • 2
  • 9
  • 4
    Is there a reason you not just link to the other site? – Ikke Mar 09 '13 at 14:39
  • ... and you are using 2x bandwidth. – AKS Mar 09 '13 at 14:42
  • 1
    The "headers already sent" error is not obvious from the code you show. What is the line PHP claims the first output is from? (Also what Ikke and Ayesh said) – Pekka Mar 09 '13 at 14:45
  • @AyeshK Much more than two times, since this doesn't handle `If-Modified-Since`. – Boann Mar 09 '13 at 14:47
  • ikke: I don't like to link because I don't want vistors/google like to know that I have to (related) websites. The photo's are the same, but the content is different (for an other audience). – luvTweb Mar 09 '13 at 15:42
  • @Boann you are correct, is there another way to display a .png file hosted on my other site without showing (in html / source code) that the picture was from site_a ? I have a collection of hundreds of photo's I like to use on both sides, but hate to copy them to both sites... (the amount is almost 1000 photo's and its increasing all the time) – luvTweb Mar 09 '13 at 16:03

2 Answers2

0

Try putting <?php ob_start(); ?> at the start of webpage and at the end put <?php ob_flush(); ?>

Hari krishnan
  • 2,060
  • 4
  • 18
  • 27
  • When doing so nothing is shown in the webbroser:chrome (only a broken link image). In firefox it says: the image "http://site_a.net/img/picture.php" cannot be displayed, because it contains errors. watch especially the php extension in ...../img/picture.php above. Strange isn't it? – luvTweb Mar 09 '13 at 15:54
  • when I only use i see the picture – luvTweb Mar 09 '13 at 15:56
0

What is the br tag doing at there ?

Here's the rules and the correct version of your code;

Header must be the first output. You can't send the headers after echoed something or after an output unless you use output buffering like Hari's suggested.

Try with

<?php
header('Content-Type: image/png');
echo(file_get_contents('http://site_a.com/img.png'));
?>
Tim
  • 699
  • 8
  • 27
  • (*"What is the br tag doing at there ?"* It was not in the original question; it was (apparently mistakenly) added by someone's edit.) – Boann Mar 09 '13 at 14:59
  • the
    was my fault when posting this message, I didn't know how to put the "echo" line on a new line, so I inserted
    now I know to use four spaces to put code in a message...
    – luvTweb Mar 09 '13 at 16:05