Download:
<?PHP$curlobj=Curl_init (); curl_setopt ($curlobj, Curlopt_url, "ftp://192.***.*.***/file name"); curl_setopt ($curlobj, Curlopt_header, 0); curl_setopt ($curlobj, Curlopt_returntransfer, 1); curl_setopt ($curlobj, Curlopt_timeout, 300);//Times out after 300scurl_setopt ($curlobj, Curlopt_userpwd, "User name: Password");//FTP username: password//sets up the output file$outfile=fopen(' Test.txt ', ' WB ');//save to local file namecurl_setopt ($curlobj, Curlopt_file,$outfile);$rtn= Curl_exec ($curlobj); fclose($outfile); if(!curl_errno ($curlobj)){ Echo"RETURN:".$rtn; } Else { Echo' Curl error: '. Curl_error ($curlobj);} Curl_close ($curlobj);
Upload:
<?PHP$curlobj=Curl_init (); $localfile= ' test.php ';$fp=fopen($localfile, ' R '); curl_setopt ($curlobj, Curlopt_url, "ftp://***.***.***.***/saved file name"); curl_setopt ($curlobj, Curlopt_header, 0); curl_setopt ($curlobj, Curlopt_returntransfer, 1); curl_setopt ($curlobj, Curlopt_timeout, 300);//Times out after 300scurl_setopt ($curlobj, Curlopt_userpwd, "User name: Password");//FTP user name: Passwordcurl_setopt ($curlobj, Curlopt_upload, 1); curl_setopt ($curlobj, Curlopt_infile,$fp); curl_setopt ($curlobj, Curlopt_infilesize,filesize($localfile));$rtn= Curl_exec ($curlobj); fclose($fp); if(!curl_errno ($curlobj)){ Echo"uploaded successfully."; } Else { Echo' Curl error: '. Curl_error ($curlobj);} Curl_close ($curlobj);
Curl Learning Notes and summaries (4) Use Curl to download files from FTP and upload files to FTP