60,));$context = stream_context_create($opts);//$strData = file_get_con">
当前位置:Gxlcms > PHP教程 > phpsftp下传上载

phpsftp下传上载

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

php sftp上传下载
  1. <!--?php
  2. //php环境中必须有ssh
  3. $strServer = "ftp.***.com.cn";
  4. $strServerPort = "22";
  5. $strServerUsername = "***";
  6. $strServerPassword = "***";
  7. //connect to server
  8. $resConnection = ssh2_connect($strServer, $strServerPort);
  9. if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword)){
  10. //init SFTP
  11. $resSFTP = ssh2_sftp($resConnection);
  12. //download a File
  13. //1
  14. $filename = 'c:/abc.jpg';
  15. $opts = array(
  16. 'http'=-->array(
  17. 'method'=>"GET",
  18. 'timeout'=>60,
  19. )
  20. );
  21. $context = stream_context_create($opts);
  22. //
  23. $strData = file_get_contents("ssh2.sftp://{$resSFTP}/dfr508/WUN/ikea-logo.jpg", false, $context);
  24. //
  25. file_put_contents($filename, $strData);
  26. //2 也可以用copy()
  27. if(!copy("ssh2.sftp://{$resSFTP}/dfr508/WUN/ikea-logo.jpg", $filename)) {
  28. echo 'download failed';
  29. }
  30. //upload a File
  31. //1
  32. //file_put_contents("ssh2.sftp://{$resSFTP}/dfr508/WUN/456.jpg", 'c:/123.jpg');
  33. //2
  34. //
  35. if(!copy("c:/abc.jpg", "ssh2.sftp://{$resSFTP}/dfr508/WUN/789.jpg")) {
  36. //
  37. echo 'upload failed';
  38. //
  39. }
  40. } else {
  41. echo "Unable to authenticate on server";
  42. }
  43. ?>

人气教程排行