How to fetch images from remote server using PHP

We were updating the products on a client website from a new supplier. The supplier provided a CSV file containing the list of products and related information however they did not provide any product images. After browsing their website I found that they had saved all of their product images by the SKU name of the product so this was not going to be difficult at all.

I first uploaded the products into the database and then set about writing the script which would allow me to fetch the product image from the remote server and save it onto our server. The following is the PHP code I wrote to perform the extraction of the images from the remote server.

//code to fetch the SKU code from the database
//loop starts here

$strContents = file_get_contents(<Image URL on the supplier website>);
$fobj = file(“images/”.<SKU code of the product>.”jpg”,”w+”);
$fobj = null;

//loop ends here

Of course the above code would require some changes in your server configuration to allow PHP to extract the image file from the remote server.

Comments? Let me know please.

Leave a Comment