当前位置:Gxlcms > PHP教程 > php简单ftp文件上传范例

php简单ftp文件上传范例

时间:2021-07-01 10:21:17 帮助过:5人阅读

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

人气教程排行