test.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. session_start();
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', 1);
  5. $test = 2;
  6. if($test == 1) {
  7. define("SFTP_HOST", "home595637013.1and1-data.host");
  8. define("SFTP_USER", "u543064336");
  9. define("SFTP_PASS", "1wx3c4cdfv6sz");
  10. define("SFTP_REMOTE", "/");
  11. } else {
  12. define("SFTP_HOST", "46.105.33.123");
  13. define("SFTP_USER", "sftp_35034");
  14. define("SFTP_PASS", "4ALSyzu43tDh5hVQ82kL29");
  15. define("SFTP_REMOTE", "/import/");
  16. }
  17. define("DOCUMENT_ROOT", "/var/www/dev.cms.cse-invent.com/");
  18. define("SFTP_LOCAL", DOCUMENT_ROOT . "datas/sftp/");
  19. $authent = "";
  20. echo SFTP_LOCAL;
  21. ///////////////////////////////////////////////////////////////////////////////////////////
  22. function testSFTP(){
  23. $originalConnectionTimeout = ini_get('default_socket_timeout');
  24. ini_set('default_socket_timeout', 2);
  25. if ($authent = @ssh2_connect(SFTP_HOST, 22)) {
  26. ini_set('default_socket_timeout', $originalConnectionTimeout);
  27. echo "<div>Accès serveur OK</div>";
  28. } else {
  29. echo "<div>Accès serveur en erreur</div>";
  30. }
  31. if (@ssh2_auth_password($authent, SFTP_USER, SFTP_PASS)) {
  32. echo "<div>Authentification OK</div>";
  33. } else {
  34. echo "<div>Authentification En erreur</div>";
  35. }
  36. if ($connexion = @ssh2_sftp($authent)) {
  37. echo "<div>Connexion OK</div>";
  38. } else {
  39. echo "<div>Connexion En erreur</div>";
  40. }
  41. $stream = @fopen("ssh2.sftp://" . $connexion . SFTP_REMOTE . "test.txt", "w");
  42. if ($stream == FALSE){
  43. echo "<div>Impossible d'écrire sur le serveur distant</div>";
  44. } else {
  45. echo "<div>Succès d'écriture sur le serveur distant</div>";
  46. }
  47. $realpath = @ssh2_sftp_realpath($connexion, SFTP_REMOTE);
  48. $dir = "ssh2.sftp://" . $connexion . $realpath;
  49. $tempArray = array();
  50. if (is_dir($dir)) {
  51. if ($dh = opendir($dir)) {
  52. while (($file = readdir($dh)) !== false) {
  53. echo $file;
  54. $tempFile = NULL;
  55. $realpath = @ssh2_sftp_realpath($connexion, $file);
  56. $filetype = filetype($dir . $realpath);
  57. $size = sprintf("%.2f", filesize($dir . $realpath));
  58. if ($filetype != "dir") {
  59. $infos = @ssh2_sftp_stat($connexion, SFTP_REMOTE.$file);
  60. $tempFile["file"] = $file;
  61. print_r($infos);
  62. //$tempFile["size"] = $infos["size"];
  63. // $tempFile["date"] = $infos["atime"];
  64. $tempArray[] = $tempFile;
  65. }
  66. }
  67. closedir($dh);
  68. }
  69. }
  70. print_r($tempArray);
  71. }
  72. testSFTP();