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