I’m trying to copy an image from a remote server with the following code:
$src = "http://www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/";
file_put_contents($dest, file_get_contents($src));
Unfortunately I keep getting the following error:
Warning: file_put_contents(/server/location/upload/) [function.file-put-contents]: failed to open stream: Is a directory in /server/location/myscript.php on line 220
Do you have any ideas how to get around this?
You need to specify the filename. I added
basename($src)which will write to the same filename that the original was. Be careful if you’re copying from other directories,basename()only returns the filename so if you copy /image.jpg and /a/image.jpg you’ll write over the original.