码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-03-29 12:06:42      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

下载:

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

 

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

标签:

原文地址:http://www.cnblogs.com/dee0912/p/4375434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!