0

With the following methods of retrieving files in PHP I keep on hitting an anti-hotlinking security picture rather than the image I'm looking for. The odd thing is, when I manually input the url in Firefox/IE or even Internet Download Manager I do get the correct file, so there must be something wrong with the methods I have tried so far.

file_put_contents:

file_put_contents($localpath, file_get_contents($remoteURL));

The following function didn't work either:

function save_image($inPath,$outPath)
{ //Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}
save_image($remoteURL,$localpath);

And fopen()

$tag = fopen($remoteURL, 'rb');

if($tag){
 while(!feof($tag)) {
     $imgt = $imgt . fread($tag, 1024);
 }
} 

and imagecreatefromjpeg() didn't do the trick either

function LoadJpeg($imgname)
{
    /* Attempt to open */
    $im = @imagecreatefromjpeg($imgname);

    /* See if it failed */
    if(!$im)
    {
        /* Create a black image */
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);

        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

        /* Output an error message */
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }

    return $im;
}

Do I have any other options?

natli
  • 3,782
  • 11
  • 51
  • 82
  • 3
    The issue is likely the value of the HTTP Referer header you send. The site is purposefully trying to prevent you from doing exactly what you're trying to do - get to an image without going through their web page first. You may also be violating their terms&conditions - you should give those a read before proceeding. – Borealid Sep 10 '11 at 22:13
  • What the value of `allow_url_fopen` in your php.ini system? – atma Sep 10 '11 at 22:28
  • @atma It's on so that can't be it – natli Sep 10 '11 at 23:50

3 Answers3

1

If this is not your website, then there is almost nothing you can do about it. The site has been set up to stop people scrapping their images or using them in their links.

You MAY however, be able to use a curl statement rather than file_get_contents, and if that doesn't work send some browser headers with the request using curl.

If this is your website, if your running on cPanel, there is an option in the security part. I forget exactly where.

MichaelH
  • 1,600
  • 3
  • 14
  • 20
1

Since you wrote that allow_url_fopen is on and function is not working, you could try the curl.

function save_image($inPath,$outPath){
    $ch = curl_init ($inPath);
    curl_setopt($ch, CURLOPT_HEADER, 0); // required
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // required for images
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // maybe redirect on other side?
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'); // or user agent checks?
    $rawdata=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($outPath)){
        @unlink($outPath);
    }
    $fp = fopen($outPath,'x');
    fwrite($fp, $rawdata);
    fclose($fp);
}
atma
  • 875
  • 7
  • 10
  • Well this is just highly strange.. still getting the 404-replacement image. Again, if I copy the link and just paste it in firefox (even on another machine entirely) I just get the normal image.. what could we be missing? – natli Sep 11 '11 at 00:21
  • The original link :) Can you give it? – atma Sep 11 '11 at 00:23
  • Afraid not, content probably conflicts with stackoverflow rules. I just think this is so weird because I'd guess all I have to do is Enter URL->Save Image like I would if I did it manually but it seems like that's exactly what we are "emulating". – natli Sep 11 '11 at 00:38
  • But what messages were from wget? Do you logged in on this site? The site is require to be logged in? – atma Sep 11 '11 at 01:09
  • No login required, I just sent the direct link to an online friend and he could get the image just fine, meaning the only thing going wrong is that the php script that delivers the image somehow detects it's a script saving the file and not a human. Wget just sais 'HTTP request sent, response 200 OK' and then it downloads 403.gif (not 404, my bad) instead of the actual image. – natli Sep 11 '11 at 01:13
  • And the last thing. Set the host `$headers = array("Host: www.imagelocation.com");` And change the CURLOPT_HTTPHEADER to `curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);` – atma Sep 11 '11 at 02:05
  • Even after that, still getting the 403 image.. this is so weird. Like I said, even adding the link in Internet Download Manager downloads the correct link, and that's not exactly a browser either. – natli Sep 11 '11 at 14:38
0

There are probably some checking on their side, they're probably getting user agent, referer. Try to use for this to fake browser's behavior

genesis
  • 50,477
  • 20
  • 96
  • 125
  • I'm assuming I need to change this; curl_setopt($ch, CURLOPT_HEADER, 0); is this correct? I'm also looking at CURLOPT_HTTPHEADER but I can't really figure out what to do with it all from the link you provided. – natli Sep 10 '11 at 23:54
  • @natli: no. CURLOPT_USERAGENT and CURLOPT_REFERER – genesis Sep 10 '11 at 23:55
  • Still getting the 404 even with useragent and referer on ;X – natli Sep 11 '11 at 00:22
  • @natli: strange. there are probably more more security checkings. – genesis Sep 11 '11 at 00:23