PHP通过FTP上传文件详解介绍

本文章来总结几个利用php中的ftp功能来实现通过FTP上传文件,有需要学习的朋友可参考参考.

ftp_get() 函数从 FTP 服务器上下载一个文件,若成功则返回 true,失败则返回 false.

语法:ftp_get(ftp_connection,local,remote,mode,resume)

ftp_connect() 函数建立一个新的 FTP 连接,若成功,则返回一个连接标识,否则返回 false.

语法:ftp_connect(host,port,timeout)

ftp_login() 函数登录 FTP 服务器,若成功则返回 true,失败则返回 false 并发出一个警告.

语法:ftp_login(ftp_connection,username,password)

三个函数己经介绍好了,现在开始了.

例1,代码如下:

  1. $ftp_server = "*.*.*.*";
  2. $ftp_user = "lu";
  3. $ftp_pass = "love you";
  4. // set up a connection or die
  5. $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
  6. $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
  7. if ((!$conn_id) || (!$login_result)) {
  8. echo "FTP connection has failed!";
  9. echo "Attempted to connect to $ftp_server for user $ftp_user_name";
  10. exit;
  11. } else {
  12. echo "Connected to $ftp_server, for user $ftp_user_name";
  13. }
  14. // try to login
  15. $filename=date('Ymd').".xml";
  16. $source_file="/usr/local/IVR/sendwireless/xml/data/".$filename; //源地址
  17. echo $source_file;
  18. $destination_file="/ITC/admin/logstat/ftplog/".$filename; //目标地址
  19. $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY) or die("Couldn't connect to $ftp_server");
  20. ftp_quit($conn_id);
  21. if (!$upload) {
  22. echo "FTP upload has failed!";
  23. } else {
  24. echo "Uploaded $source_file to $ftp_server as $destination_file";
  25. }
  26. ftp_close($conn_id);

上传时先传至本地对文件作必要的修改,如加水印等等操作,然后再通过FTP传至远程服务器.

例2,代码如下:

  1. //上传图片
  2. if ($_FILES['pic']['name'])
  3. {
  4. $file_path='/opt/www/img/';
  5. $pic = upload('pic', $filename, 'jpg|jpeg|gif|bmp|png', $file_path);
  6. if(!$pic)
  7. {
  8. echo "图片上传失败!";
  9. exit;
  10. }
  11. require_once(ROOT_PATH . 'Lib/Class/Ftp.class.php');
  12. $ftp = new ftp("127.0.0.1","gamezeroftp","123456","/opt/www");
  13. $localfile='/opt/www/img/'.$pic;
  14. $remotefile='/opt/www/gamepics/'.$pic;
  15. $ftpput = $ftp->put($localfile, $remotefile); //FTP上传原图到远程服务器
  16. if(!$ftpput){
  17. echo "上传图片到远程服务器失败!";
  18. }
  19. $ftp->bye(); //关闭FTP连接
  20. }

附上FTP操作类,代码如下:

  1. ftpUrl=$ftpUrl;
  2. }
  3. if($ftpUser){
  4. $this->ftpUser=$ftpUser;
  5. }
  6. if($ftpPass){
  7. $this->ftpPass=$ftpPass;
  8. }
  9. if($ftpUrl){
  10. $this->ftpDir=$ftpDir;
  11. }
  12. if ($this->ftpR = ftp_connect($this->ftpUrl, 21)) {
  13. if (ftp_login($this->ftpR, $this->ftpUser, $this->ftpPass)) {
  14. if (!emptyempty($this->ftpDir)) {
  15. ftp_chdir($this->ftpR, $this->ftpDir);
  16. }
  17. ftp_pasv($this->ftpR, true);//R 启用被动模式;
  18. $status = 1;
  19. } else {
  20. $status = 3;
  21. }
  22. } else {
  23. $status = 2;
  24. }
  25. }
  26. //R 切换目录;
  27. function cd($dir) {
  28. return ftp_chdir($this->ftpR, $dir);
  29. }
  30. //R 返回当前路劲;
  31. function pwd() {
  32. return ftp_pwd($this->ftpR);
  33. }
  34. //R 创建目录
  35. function mkdir($directory) {
  36. return ftp_mkdir($this->ftpR,$directory);
  37. }
  38. //R 删除目录
  39. function rmdir($directory) {
  40. return ftp_rmdir($this->ftpR,$directory);
  41. }
  42. //R 上传文件;
  43. function put($localFile, $remoteFile = '') {
  44. if ($remoteFile == '') {
  45. $remoteFile = end(explode('/', $localFile));
  46. }
  47. $res = ftp_nb_put($this->ftpR, $remoteFile, $localFile, FTP_BINARY);
  48. while ($res == FTP_MOREDATA) {
  49. $res = ftp_nb_continue($this->ftpR);
  50. }
  51. if ($res == FTP_FINISHED) {
  52. return true;
  53. } elseif ($res == FTP_FAILED) {
  54. return false;
  55. }
  56. }
  57. //R 下载文件;
  58. function get($remoteFile, $localFile = '') {
  59. if ($localFile == '') {
  60. $localFile = end(explode('/', $remoteFile));
  61. }
  62. if (ftp_get($this->ftpR, $localFile, $remoteFile, FTP_BINARY)) {
  63. $flag = true;
  64. } else {
  65. $flag = false;
  66. }
  67. return $flag;
  68. }
  69. //R 文件大小;
  70. function size($file) {
  71. return ftp_size($this->ftpR, $file);
  72. }
  73. //R 文件是否存在;
  74. function isFile($file) {
  75. if ($this->size($file) >= 0) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. //R 文件时间
  82. function fileTime($file) {
  83. return ftp_mdtm($this->ftpR, $file);
  84. }
  85. //R 删除文件;
  86. function unlink($file) {
  87. return ftp_delete($this->ftpR, $file);
  88. }//开源软件:phpfensi.com
  89. function nlist($dir = '/service/resource/') {
  90. return ftp_nlist($this->ftpR, $dir);
  91. }
  92. //R 关闭连接;
  93. function bye() {
  94. return ftp_close($this->ftpR);
  95. }
  96. }