Hi,

I'm trying to grab an image from remote URLs and save it onto my local server.

And this is the code created. It seems that the image data is not being copied from the remote server.

<?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!";
} 
?>

If i replace

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

with

$img = "test.jpg";

the image appears.

However, when I use an URL, the image generated is empty (0 KšŸ˜Ž.

Anyone know what is the problem here? And if so, is there another way to resolve this, either by using Http streams or...?

Any help would be great

    Turn on all error reporting and see if there are any "interesting" messages (such as allow_url_fopen not being allowed):

    <?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    // . . . rest of script . . .
    ?>
    

    PS: Duplicate threads merged.

      Warning:file_get_contents(http%3A%2F%2Fwww.myserver.com%2Ftest.jpg) [function.file-get-contents]:failed to open stream: No such file or directory in /home/vol5/byethost13.com/b13_1642225/htdocs/Final/imagecopy.php

      Hmm... anyone knows what's the problem?

      I'm quite the beginner in PHP coding. The image URLs in question can be read via a web browser and saved as. So can I safely assume that these images can be read using this method?

        Get rid of the call to urlencode(), and I think you'll possibly be OK, then (again assuming that allow_url_fopen is enabled on your host).

        PS: urlencode() is normally only needed for values used in a URL query string (the part after a "?" in a URI).

          Alright, allow_url_fopen is disabled on the host. Now I know what's the problem.

          Thanks for the GREAT help =)

            You may need to use [man]cURL[/man] then to access off-site files.

              Write a Reply...