Download file using curl in php


You would need to feed CURLOPT_URL the full URL to the file. Also if you want to download a file you might want to save it somewhere.

Working example:
$curl = curl_init();
$file = fopen("ls-lR.gz", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.sunet.se/ls-lR.gz"); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_exec($curl);
curl_close($curl);
fclose($file);



I used the ftp_* functions for ftp, in place of curl, because curl 
tried to change directories, but its not allowed on this ftp server, so 
the code i used is;
$local_file = 'output.rar';
$server_file = '/somedir/1/bar/foo/somearchive.rar';
$ftp_user_name='anonymous';
$ftp_user_pass='mozilla@example.com';
$ftp_server='server.host';
// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

/* uncomment if you need to change directories
if (ftp_chdir($conn_id, "<directory>")) {
    echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else { 
    echo "Couldn't change directory\n";
}
*/

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
} else {
    echo "There was a problem\n";
}

// close the connection
ftp_close($conn_id);
It works !!
Related Posts:
  • Php Networking Functions There are many functions in php to convert or look up domain name, IP address, protocol, and service information. Domain name/IP address lookups and conversions gethostbynamel() gethostbyname() gethostbyaddr() … Read More
  • PHP variable-related functions Cast a value from one type to other type:- doubleval(), intval(), strval() Set  type of a variable:- settype() Verify  the type of a variable:- is_array(), is_bool(), is_double(), is_float(), is_int(), … Read More
  • PHP-Renaming Files and Directories Renaming  Files and Directories There are a few useful file and directory functions built-in to PHP which I'll cover in brief here. They include renaming and deleting files as well as listing files located in a… Read More
  • IT company in kolkata Matrix Technologies Pvt. Ltd.8/1C, Chowringhee Lane, Room No. 2E, 2nd flKolkata7000162252 8/29 Media Software61A, S.N.Roy RdKolkata70003424479580 Metalogic Systems Pvt. LtdPlot-J1/1, Block-EP and GP, Sector-V, Salt LakeK… Read More
  • Drupal Bootstrap Process Drupal Bootstrap Process? Drupal bootstraps itself on every request by going through a series of bootstrap phases. These phases aredefined in bootstrap.inc and proceed. Configuration Sets global variables used throughout th… Read More
  • Php Operators PHP Operators The following table lists the operators from highest to lowest precedence. Operators A !, ~, ++, --, @ Right *, /, % Left +, -, . Left <<, >> Left <… Read More
  • php-use the header() function-php An HTTP (HyperText Transfer Protocol) header is used to send information back and forth between the server and the client (the Web browser). Normally this information is in the form of HTML, which is why the address for Web … Read More
  • php implode array elements php implode array elements Join array elements with a string string. implode(string, arraypieces) Returns a string containing a string representation of all the array elements in the same order,  string between each e… Read More
  • Advanced Database Techniques PEAR DB is the database primitives shown earlier; it provides several shortcut functions for fetching result rows, as well as a unique row ID system and separate prepare/execute steps that can improve the performance of re… Read More
  • php-static variables A static variable retains its value between calls to a function but is visible only within that function. IT declare a variable static with the static keyword. function test_counter ( ) { static $counter = 0; $counte… Read More
  • php best tutorial-php variables PHP automatically creates variables for all the data it receives in an HTTP request. This can include GET data, POST data, cookie data, and environment variables. The variables are either in PHP's global symbol table or … Read More
  • Web-Based MySQL Administration Web-Based MySQL Administration At http://phpwizard.net/phpMyAdmin you will find a PHP Web application that can help you manage MySQL. It enables you to manage a MySQL database using a browser and makes administrative work… Read More
  • php-Error Control Operators PHP gives one error control operator: the at sign (@).Any error messages that might be generated by that expression will be ignored. If you have set a custom error handler function with set_error_handler() then it will s… Read More
  • pass information from page to page-best seo Adding information to the URL: You can add certain information to the end of the URL of the new page, and PHP puts the information into builtin arrays that you can use in the new page. This method is most appropriate when yo… Read More
  • php urlencode - Make a strong GET Query String  php urlencode URL-encodes a string, converting spaces into plus (+ ) signs. urlencode() makes a string safe to use as part of a URL. It does this by encoding every character within the string that may be misi… Read More