I was working on a project where I was using an API to connect to Amazon to pull book details via ISBN. I needed to store the remote images on my server to be associated with the product that was being created and I put together these few lines of code to do so. It obviously will need to be reworked for your own needs, but the logic is below.

If you wanted to use directories on server you could (remove the last line above and) do something like

This WordPress code tag BS is pissing me off and I will be finding a plugin to allow all this to be copied and pasted easily 🙂