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).

Link to comment
Share on other sites

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

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.