Sindor Posted March 17, 2008 Share Posted March 17, 2008 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). Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 17, 2008 Share Posted March 17, 2008 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 Quote Link to comment Share on other sites More sharing options...
corbin Posted March 17, 2008 Share Posted March 17, 2008 Try it without the urlencode(). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.