cURL 学习笔记与总结(4)使用 curl 从 ftp 上下载文件与上传文件到 ftp

下载:

<?php
$curlobj = curl_init();    
curl_setopt($curlobj, CURLOPT_URL, "ftp://192.***.*.***/文件名");  
curl_setopt($curlobj, CURLOPT_HEADER, 0); 
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); // times out after 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "用户名:密码");//FTP用户名:密码
// Sets up the output file
$outfile = fopen(‘test.txt‘, ‘wb‘);//保存到本地的文件名
curl_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);

上传:

<?php
$curlobj = curl_init();    
$localfile = ‘test.php‘;
$fp = fopen($localfile, ‘r‘);

curl_setopt($curlobj, CURLOPT_URL, "ftp://***.***.***.***/保存的文件名");  
curl_setopt($curlobj, CURLOPT_HEADER, 0); 
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); // times out after 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "用户名:密码");//FTP用户名:密码

curl_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);

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。