Home > Backend Development > PHP Tutorial > PHP simple ftp file upload example

PHP simple ftp file upload example

WBOY
Release: 2016-07-25 08:43:12
Original
1188 people have browsed it

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 666n";
  32. } else {
  33. print "could not chmod $filen";
  34. }
  35. // close the FTP stream
  36. ftp_close($conn_id);
复制代码

文件上传, php, ftp


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template