07 August 2014

PHP code for fetching a remote file and saving it with the right file name

Here is some PHP code I wrote for downloading a file from a remote server and saving it locally using the file's real name.
It tries to get the real file name using three methods:
  1. First by checking if the server returned the name of the original file to save as (using the content-disposition http header)
  2. By checking the content-type header that the server returned
  3. If all else fails then by the filename in the requested url.
I hope you find this useful.
Share: