PHP Download Image Or File From URL
Posted by in PHP August 31, 2011 19 Comments

I’ll show you 3 functions that download a particular file (ex: image,video,zip,pdf,doc,xls,etc) from a remote resource (via a valid URL) then save to your server.

Depending on your current php.ini settings, some functions may not work; therefore, let try which function is best for you.

Note: please ensure the folder you want to store the downloaded file is existed and has write permission for everyone or the web process.

For all examples below, I assume that we’re going to download a remote image from: http://4rapiddev.com/wp-includes/images/logo.jpg and save it to a download sub folder with name: file.jpg.

1. PHP Download Remote File With file_get_contents and file_put_contents

<?php
	function download_remote_file($file_url, $save_to)
	{
		$content = file_get_contents($file_url);
		file_put_contents($save_to, $content);
	}
?>

Example:

<?php
	download_remote_file('http://cdn2.4rapiddev.com/wp-includes/images/logo.jpg', realpath("./downloads") . '/file.jpg');
?>

2. PHP Download Remote File With CURL

<?php
	function download_remote_file_with_curl($file_url, $save_to)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 0); 
		curl_setopt($ch,CURLOPT_URL,$file_url); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		$file_content = curl_exec($ch);
		curl_close($ch);
 
		$downloaded_file = fopen($save_to, 'w');
		fwrite($downloaded_file, $file_content);
		fclose($downloaded_file);
 
	}
?>

Example:

<?php
	download_remote_file_with_curl('http://cdn2.4rapiddev.com/wp-includes/images/logo.jpg', realpath("./downloads") . '/file.jpg');
?>

3. PHP Download Remote File With fopen

<?php
	function download_remote_file_with_fopen($file_url, $save_to)
	{
		$in=    fopen($file_url, "rb");
		$out=   fopen($save_to, "wb");
 
		while ($chunk = fread($in,8192))
		{
			fwrite($out, $chunk, 8192);
		}
 
		fclose($in);
		fclose($out);
	}
?>

Example:

<?php
	download_remote_file_with_fopen('http://cdn2.4rapiddev.com/wp-includes/images/logo.jpg', realpath("./downloads") . '/file.jpg');
?>

Again, “download” folder must be exists and writable.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.herculessport.com India Sports

    This is very great information about this articles..this is very informative post..

  • Pingback: PHP Get Image Width And Height | 4 Rapid Development

  • http://www.google.com/ Evaline

    Gee wliilkres, that’s such a great post!

    • mladen

      I dont understand what exactly input in “$save_to” Here I try this:
      where is error?

  • mladen

    I dont understand what exactly input in “$save_to” Here I try this:
    where is error?

    thanx

    • hoanhuynh

      Hi mladen, you need to pass a real location to save the image on your server/computer to $save_to parameter, ex: D:/htdocs/download/file.jpg

  • mladen

    Ill replace “$save_to” with my link to folder on my server betwen ‘ ‘
    ‘http://www.c-bit.hr/1/ASBIS/download/’
    and download folder have permision 777
    file url is replaced with ‘http://cdn2.4rapiddev.com/wp-includes/images/logo.jpg’
    no result

    • hoanhuynh

      Hi,it shoud be a phiscical path on your server,not url path. Hope it helps. H2

  • mladen

    Thanx for answer
    but I dont know this path,, My directory “sranje” is in public_html folder and “skidaj.php” is also in public_html what is phisical path in my case
    (‘http://cdn2.4rapiddev.com/wp-includes/images/logo.jpg’, ‘./home/cbithr/public_html/sranje’ )
    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘&’ or T_VARIABLE in /home/cbithr/public_html/skidaj.php on line 2

    should I replace $content with name of image or what ? Im talking about first example

    • hoanhuynh

      Dear mladen,

      Let’s try with this:

      download_remote_file(‘http://cdn2.4rapiddev.com/wp-includes/images/logo.jpg’, ‘/home/cbithr/public_html/sranje/download/file.jpg’);

      … and also make sure you already added the function definition.

      Good luck,
      H2.

      • mladen

        Thanx hoanhuynh now works.
        In case a got several link and want to download all links(pictures.jpg) in one folder with existing names
        how to define $save_to link in this case ?
        Ill try replace $save_to with folder path but without result
        /home/cbithr/public_html/sranje/download/

  • mladen

    works with PHP Download Remote File With fopen
    /home/cbithr/public_html/sranje/download/.$name_of_string
    Thanx

  • Greg Newman

    Nice post – a quick additional related question: What can one do when the $file_url is not fully qualified and does not directly point to a file.zip but rather points to a page that itself forces download of a file.zip. My example URL is:

    http://ge.ssec.wisc.edu/modis-today/index.php?gis=true&filename=t1_12019_USA2_143_250m&date=2012_01_19_019&product=true_color&resolution=250m&overlay_sector=false&overlay_state=true&overlay_coastline=true

    Any help greatly approeciated!

    • hoanhuynh

      Hi Greg Newman,

      Thanks for your good question, below is a answer for your question:

      1. Download that file and name it whatever you want, for example: file.jpg. I tried with PHP CURL, option 2.

      2. Use PHP function: mime_content_type to determine its mine type.

      ———————
      echo mime_content_type(realpath(“./downloads”) . ‘/file.jpg’); //return: application/zip in this case
      ———————

      3. Base on its mine type, use another PHP function to rename it to a corresponding extension (zip).

      Hope it’s clear.

      H2.

  • Greg Newman

    Forgot to check Notify Me.

    • hoanhuynh

      Me again ;)

      Don’t understand your comment.

      H2.

      • Greg Newman

        Hi H2,

        Thanks for your insight (a[pologies for my cryptic second post). I’ll try as you suggest, using PHP CURL Option 2 to download the file. It is at this step that I think I am having trouble… is there specific header declarations that will be important for option 2 to force a download of the file from the webiste I listed?

        Again – thank you! More soon, Greg

  • Noemail

    Great! Thanks! 

  • Emraan Rehman

    Itz very Helpfull, Thank guys. Allah Bless u.