Jump to content

Pulling image from remote server to local server


Sindor

Recommended Posts

Hello,

 

    I'm trying to pull remote images from URLs to be saved onto my local server.

 

However, the resulting image is of 0 KB...

 

My code is as follows:

 

<?php

//original image
$img = "http://www.myserver.com/cat.jpg";

//directory to copy to (must be CHMOD to 777)
$copydir = "images//";

$data = file_get_contents(urlencode($img),FILE_BINARY);
if( $data )
{
        $file = fopen($copydir . "test.jpg", "w+");
        fputs($file, $data);
        fclose($file);
}
else
{
        echo "file_get_contents() failed!";
} 
?>

 

When I replace

 

$img = "http://www.myserver.com/cat.jpg";

 

with

 

$img = 'test.jpg';

 

it works.

 

Does anyone know how to resolve this for a remote issue? Or if another way is needed (i.e. using http streams or some other method).

I really dont like using file_get_contents for anything but text files, even with the binary flag.

 

Try the same with with fopen(), fread()/stream_get_contents(), flcose() instead.

 

If that doesn't work, you host may be disabling fopen wrappers

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.