php 簡單ftp檔案上傳範例

來源:互聯網
上載者:User

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
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.