WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Using cURL to download a file programmatically.
Categories : PHP, PHP Extensions, CURL Click here to Update Your Picture
bastien koert
Date : Sep 17th 2004
Grade : 4 of 5 (graded 5 times)
Viewed : 48592
File : No file for this code example.
Images : No Images for this code example.
Search : More code by bastien koert
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Using cURL (PHP's client URL extension) to download a file programmatically.

Hi All,

Here is a simple function using cURL to download a remote file. My goal here
was to access a remote wav file hosted on another server and import it into my
system.

Steps:
1. Enable cURL (see http://ca3.php.net/manual/en/ref.curl.php).
For windows: In order to enable this module on a Windows environment, you must copy
libeay32.dll and ssleay32.dll from the DLL folder of the PHP/Win32 binary
package to the SYSTEM folder of your Windows machine.
(Ex: C:\WINNT\SYSTEM32 or C:\WINDOWS\SYSTEM)

2. Uncomment the cURL extension in the php.ini file:
extension=php_curl.dll

Functionality:
1. Its a function and accepts three parameters (you can, of course, change this)
get_file1($file, $local_path, $newfilename)
$file : is the filename of the object to be retrieved
$local_path : is the local path to the directory to store the object
$newfilename : is the new file name on the local system

2. To use it:

   <?php
   $wav_file
= get_file1($filename, $local_path, $newfilename);
   
?>

cURL returns true on success and false on failure.

The function:

<?php
function get_file1($file, $local_path, $newfilename)
{
   
$err_msg = '';
    echo
"<br>Attempting message download for $file<br>";
   
$out = fopen($newfilename, 'wb');
    if (
$out == FALSE){
      print
"File not opened<br>";
      exit;
    }
   
   
$ch = curl_init();
           
   
curl_setopt($ch, CURLOPT_FILE, $out);
   
curl_setopt($ch, CURLOPT_HEADER, 0);
   
curl_setopt($ch, CURLOPT_URL, $file);
               
   
curl_exec($ch);
    echo
"<br>Error is : ".curl_error ( $ch);
   
   
curl_close($ch);
   
//fclose($handle);

}//end function
?>

Happy coding.



Audio-Enable Discussion Boards with this Web-page Audio Recorder / Player!
Categories : PHP Extensions, Java, PHP
PHP Youtube Downloader - This is a set of PHP functions that can be used to download movies from Youtube.com.
Categories : PHP, CURL, Regexps
A function to check if a URL exists
Categories : PHP, CURL, HTTP
Returns Yahoo! Address Book and Messenger List as an Array
Categories : PHP, PHP Classes, CURL
Sample AIM (Advanced Integration Method) PHP Script For Authorize.net using CURL
Categories : CURL, Ecommerce, PHP
Link Manager for Link Exchangers
Categories : PHP, PHP Classes, Databases, MySQL, CURL
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Open remote https url using Curl and accept cookie
Categories : PHP, CURL
Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +)
Categories : PHP, Ecommerce, XML, Web Services, CURL
These PHP Classes Check if a host is alive using various methods.
Categories : PHP, PHP Classes, Sockets, CURL
Import the yahoo address book.
Categories : PHP, CURL, Authentication
Using data from a string.
Categories : PHP, Strings, CURL
Url To Pdf Report By Remote Application
Categories : PHP, PHP Classes, PDF, CURL
Newbie Notes #8 - A cron trick
Categories : PHP, CURL, Beginner Guides
curl_close -- Close a CURL session
Categories : PHP, PHP Functions, CURL
 Mark Dhas wrote :1812
I don't believe this code actually uses the localpath 
variable, so by default this file should save to the cwd 
(current working directory).

To save the file to the directory of your choosing change 
the following line

<?php
$out 
fopen($newfilename,"wb");
?>

to 

<?php
$out 
fopen($localpath.$newfilename,"wb");
?>