Copying files from server to server using PHP and CURL

Russell Japheth
PH DevConnect
Published in
3 min readJun 7, 2017

Before we begin you’ll need a basic understanding of PHP and a little knowledge about cURL wouldn’t hurt.

What is curl?

cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. PHP implements libcurl which is is a product of cURL. Libcurl is a free client-side URL transfer library, supporting cookies, FTP, FTPS, HTTP, HTTPS, IMAP etc.

Source: https://en.wikipedia.org/wiki/CURL

We’ve all at some point needed to copy a file (or a bunch of files) from one server to the other and really didn’t want to download it just to upload it again, or there is an image you just got off a Google search and would like to include in your web site or project, granted PHP has a couple of functions like `copy()` and `file_get_contents()` but using these might not always be as convenient or as reliable (especially when you’ve got page redirects) as you would like.

So in this post we’re basically gonna build something like a MADS (Modify, Add, Delete, Show) app to do that for us.

Here is the downloadFileclass we’ll be using. It will:

  • get the name of the file from the url
  • check if a file with that name already exists and add a time-stamp if it does and then copy the file and return info about the file and where its stored

First off well create a form.

IMPORTANT: The form method must be set to method="post" else you may get a 403 error on some servers due mod_security rules!

Next well check if the form has been submitted

If the form has been submitted,

  • We will set the max_execution_time to 0 so we can copy large files
  • Next we’ll include the download file class and create an instance of it
  • Then we’ll use the getSize() method to get the size of the file
  • Now here is where we actually copy the file, we’ll use the saveFile() method to do this
  • finally we’ll log the download using the logDownload() method and print out the returned data and the file’s size.

Here is the completed script

In the example above $returnData is an array that contains the data returned by the script.

  • $returnData[0] Returns true or false depending on the result of the curl_exec() function
  • $returnData[1] is the name of the directory where the file is stored
  • $returnData[2] is the url that was copied
  • $returnData[3] is the name of the stored file
  • $returnData[4] is the path to the new file

You can use the url of an image from pexels.com (eg. https://images.pexels.com/photos/92904/pexels-photo-92904.jpeg?h=350&auto=compress&cs=tinysrgb)to try it out. Did you notice that when you change h=350 in the url, the copied file has a different size?

Remember this was done using just PHP so feel free to customize or improve it till it suits whatever you want!

Update: While this script is great for getting to know how CURL works. For your projects I’d recommend php-curl-class or guzzle.

PHDevConnect is a free, open community of Port Harcourt based Developers, UI / UX Designers, Project managers, Architects etc who can create their own digital team or join other teams for paid/open sourced projects.

--

--

Russell Japheth
PH DevConnect

Software Developer | StackOverflow Expert | General Food Advocate 😋 | Technical Writer