How to save an image from a URL in PHP

Today we’ll explain to you how to save an image from a URL in PHP. Sometimes we need to download images from a remote server and use them in projects. Therefore we will show you a few different ways to save an Image to the folder using PHP.

Two ways to save image from URL in PHP

  1. Using basic file handling function
  2. Using cURL function

1. Using basic file handling function

It is a very simple way to save an image from the URL using PHP functions file_get_contents() and file_put_contents().

  • file_get_contents(): This function is used to read file from URL and return as a string.
  • file_put_contents(): This function is used to write a string to a file.

Click here to check if an image exists from a URL in PHP.

2. Using cURL function

We can use the cURL function to copy an image from URL in PHP as shown in the code below.

That’s it for today.
Thank you for reading. Happy Coding..!!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *