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

php 简单ftp文件上传范例

程序员文章站 2022-05-17 15:49:58
...

php上传单个文件到ftp服务器的演示范例

  1. // FTP access parameters
  2. $host = 'ftp.example.org';
  3. $usr = 'example_user';
  4. $pwd = 'example_password';
  5. // file to move:
  6. $local_file = './example.txt';
  7. $ftp_path = '/data/example.txt';
  8. // connect to FTP server (port 21)
  9. $conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
  10. // send access parameters
  11. ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
  12. // turn on passive mode transfers (some servers need this)
  13. // ftp_pasv ($conn_id, true);
  14. // perform file upload
  15. $upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
  16. // check upload status:
  17. print (!$upload) ? 'Cannot upload' : 'Upload complete';
  18. print "\n";
  19. /*
  20. ** Chmod the file (just as example)
  21. */
  22. // If you are using PHP4 then you need to use this code:
  23. // (because the "ftp_chmod" command is just available in PHP5+)
  24. if (!function_exists('ftp_chmod')) {
  25. function ftp_chmod($ftp_stream, $mode, $filename){
  26. return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
  27. }
  28. }
  29. // try to chmod the new file to 666 (writeable)
  30. if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) {
  31. print $ftp_path . " chmoded successfully to 666\n";
  32. } else {
  33. print "could not chmod $file\n";
  34. }
  35. // close the FTP stream
  36. ftp_close($conn_id);
复制代码

文件上传, php, ftp