- 论坛徽章:
- 0
|
从FTP获取自动下载文件的PHP类
Php代码- 1./**
- 2. * 从FTP获取自动下载文件的PHP类
- 3. * @author yukar ( http://yukar.iteye.com )
- 4. */
- 5.class FtpClass{
- 6. private $ftpObj;
- 7. private $ftpHost = "****"; // 服务器地址
- 8. private $ftpPort = ****; // 服务器端口
- 9. private $ftpUser = "****"; // 用户名
- 10. private $ftpPassword = "****"; // 口令
- 11. private $localBase = '/your/save/path'; //你存放的目录
- 12. function __construct($initData=array()){
- 13. if (isset($initData['ftpHost']) && $initData['ftpHost']){
- 14. $this->ftpHost = $initData['ftpHost'];
- 15. }
- 16. if (isset($initData['ftpPort']) && $initData['ftpPort']){
- 17. $this->ftpPort = $initData['ftpPort'];
- 18. }
- 19. if (isset($initData['ftpUser']) && $initData['ftpUser']){
- 20. $this->ftpUser = $initData['ftpUser'];
- 21. }
- 22. if (isset($initData['ftpPassword']) && $initData['ftpPassword']){
- 23. $this->ftpPassword = $initData['ftpPassword'];
- 24. }
- 25. if (isset($initData['localBase']) && $initData['localBase']){
- 26. $this->localBase = $initData['localBase'];
- 27. }
- 28. }
- 29.
- 30. function ftp_connect(){
- 31. if(!$this->ftpObj){
- 32. $this->ftpObj = ftp_connect($this->ftpHost,$this->ftpPort); // 连接ftp服务器
- 33. if($this->ftpObj) {
- 34. if(ftp_login($this->ftpObj, $this->ftpUser, $this->ftpPassword)) { // 登录
- 35. return true;
- 36. }else{
- 37. return false;
- 38. }
- 39. }else {
- 40. return false;
- 41. }
- 42. }
- 43. }
- 44.
- 45. function ftp_download_file($fileName) {
- 46. //获取FTP路径
- 47. $ftpPath = dirname($fileName) . "/";
- 48. //获取文件名
- 49. $selectFile = basename($fileName);
- 50. // 进入指定路径
- 51. if(@ftp_chdir($this->ftpObj,$ftpPath)) {
- 52. //$localBase 如果不存在,新创建目录,务必确保 有创建权限
- 53. if(!is_dir($this->localBase)){
- 54. mkdir($this->localBase, 0777);
- 55. }
- 56. $toFile = $this->localBase.$fileName;
- 57. if(ftp_get($this->ftpObj, $toFile, $selectFile, FTP_BINARY)) { //下载指定的FTP文件到指定的本地文件
- 58. return true;
- 59. }else {
- 60. return false;
- 61. }
- 62. }else {
- 63. return false;
- 64. }
- 65. }
- 66.
- 67. function ftp_quit(){
- 68. if($this->ftpObj){
- 69. ftp_quit($this->ftpObj);
- 70. }
- 71. }
- 72.
- 73.}
- /**
- * 从FTP获取自动下载文件的PHP类
- * @author yukar ( http://yukar.iteye.com )
- */
- class FtpClass{
- private $ftpObj;
- private $ftpHost = "****"; // 服务器地址
- private $ftpPort = ****; // 服务器端口
- private $ftpUser = "****"; // 用户名
- private $ftpPassword = "****"; // 口令
- private $localBase = '/your/save/path'; //你存放的目录
- function __construct($initData=array()){
- if (isset($initData['ftpHost']) && $initData['ftpHost']){
- $this->ftpHost = $initData['ftpHost'];
- }
- if (isset($initData['ftpPort']) && $initData['ftpPort']){
- $this->ftpPort = $initData['ftpPort'];
- }
- if (isset($initData['ftpUser']) && $initData['ftpUser']){
- $this->ftpUser = $initData['ftpUser'];
- }
- if (isset($initData['ftpPassword']) && $initData['ftpPassword']){
- $this->ftpPassword = $initData['ftpPassword'];
- }
- if (isset($initData['localBase']) && $initData['localBase']){
- $this->localBase = $initData['localBase'];
- }
- }
-
- function ftp_connect(){
- if(!$this->ftpObj){
- $this->ftpObj = ftp_connect($this->ftpHost,$this->ftpPort); // 连接ftp服务器
- if($this->ftpObj) {
- if(ftp_login($this->ftpObj, $this->ftpUser, $this->ftpPassword)) { // 登录
- return true;
- }else{
- return false;
- }
- }else {
- return false;
- }
- }
- }
-
- function ftp_download_file($fileName) {
- //获取FTP路径
- $ftpPath = dirname($fileName) . "/";
- //获取文件名
- $selectFile = basename($fileName);
- // 进入指定路径
- if(@ftp_chdir($this->ftpObj,$ftpPath)) {
- //$localBase 如果不存在,新创建目录,务必确保 有创建权限
- if(!is_dir($this->localBase)){
- mkdir($this->localBase, 0777);
- }
- $toFile = $this->localBase.$fileName;
- if(ftp_get($this->ftpObj, $toFile, $selectFile, FTP_BINARY)) { //下载指定的FTP文件到指定的本地文件
- return true;
- }else {
- return false;
- }
- }else {
- return false;
- }
- }
-
- function ftp_quit(){
- if($this->ftpObj){
- ftp_quit($this->ftpObj);
- }
- }
- }
复制代码 调用就更简单了
Php代码- 1.$ftpObj = new FtpClass();
- 2.$ftpObj->ftp_connect();
- 3.$filename = '/config/config.sample.php';
- 4.$ftpObj->ftp_download_file($filename);
- 5.$ftpObj->ftp_quit();
- 6.exit();
复制代码 |
|