Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP Download Image Or File From URL

PHP Download Image Or File From URL

I’ll show you 3 php 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.

Download file from URL with PHP

For all examples below, I assume that we’re going to download a remote image from URL: 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 From URL 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);
	}
?>

<?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://4rapiddev.com/wp-includes/images/logo.jpg', realpath("./downloads") . '/file.jpg');
?>

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

2. PHP Download Remote File From URL 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);
 
	}
?>

<?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://4rapiddev.com/wp-includes/images/logo.jpg', realpath("./downloads") . '/file.jpg');
?>

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

3. PHP Download Remote File From URL 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);
	}
?>

<?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://4rapiddev.com/wp-includes/images/logo.jpg', realpath("./downloads") . '/file.jpg');
?>

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

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

Aug 31, 2011Hoan Huynh
How To Save PHP Error Log To File In IISFree Cloud Server For Testing
You Might Also Like:
  • ASP.Net C# Download Or Save Image File From URL
  • PHP Save String Content To File
  • Get File Mime Type Using PHP
  • PHP Read File Using Fread() Simple Example
  • View Download List In Google Chrome And Firefox
  • PHP Get Remote Image Width Height
  • PHP Delete File Function
  • PHP Get Image Width And Height
  • Manage MacBook Hosts File Easier With Hosts Widget
  • FancyBox Redirect To A Predefined URL When Click On Image
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

Link9 years ago PHPcURL, file_get_contents, file_put_contents, fopen, Fread, fwrite21,949
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
21,949 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
19,795 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,618 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,129 views
C# Read Json From URL And Parse/Deserialize Json
9,586 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Buy Research Papers – How to See Them at a Bargain Price
  • How to Write a Research Paper For Sale
  • Essay Writing – Some Useful Suggestions for Writing Urgent Essays
  • Essay Writing Service Tips For The Online Essay
  • Research Paper Writing Service
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (67)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development