欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php使用ftp函数实现简单上传功能

程序员文章站 2022-06-07 10:14:28
...
  1. /**

  2. ftp上传文件
  3. link:bbs.it-home.org
  4. date:2013/2/25
  5. */
  6. $ftp_server = "192.168.8.8";
  7. $ftp_user = "test123";
  8. $ftp_pass = "yourpassword";
  9. // set up a connection or die

  10. $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
  11. $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

  12. if ((!$conn_id) || (!$login_result)) {

  13. echo "FTP connection has failed!";
  14. echo "Attempted to connect to $ftp_server for user $ftp_user_name";
  15. exit;
  16. } else {
  17. echo "Connected to $ftp_server, for user $ftp_user_name";
  18. }
  19. // try to login

  20. $filename=date('Ymd').".xml";
  21. $source_file="/var/mydata/".$filename; //源地址
  22. echo $source_file;
  23. $destination_file="/admin/ftplog/".$filename; //目标地址
  24. $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY) or die("Couldn't connect to $ftp_server");
  25. ftp_quit($conn_id);
  26. if (!$upload) {

  27. echo "FTP upload has failed!";
  28. } else {
  29. echo "Uploaded $source_file to $ftp_server as $destination_file";
  30. }
  31. ftp_close($conn_id);
  32. ?>
复制代码