PHP Simple FTP File Upload example

Source: Internet
Author: User
Tags ftp file ftp access

PHP upload a single file to the FTP server demo example

  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) 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 is 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);
Copy Code
File Upload, PHP, FTP
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.