I use the following code, but what happens when the target image is not a jpg ? How can I make sure that any format will be saved correctly?
myimage
will be myimage[$i]
because I will save a lot of images.
$content = file_get_contents("http://anothersite/images/goods.jpg");
file_put_contents(YOUR_DIRECTORY."myimage.jpg",$content);
You can get the extension from the source path using pathinfo
:
$info = pathinfo("http://anothersite/images/goods.jpg");
$destination = YOUR_DIRECTORY . "myimage.{$info['extension']}";