Witam jestem tu nowy i zwracam się z prośbą o sprawdzenie kodu:

  1. <?php
  2.  
  3. # Step 6
  4. // *** Define your host, username, and password
  5. define('FTP_HOST', 'xxxx');
  6. define('FTP_USER', 'xxxx');
  7. define('FTP_PASS', 'xxxx');
  8.  
  9.  
  10. // *** Include the class
  11. include('ftp_class.php');
  12.  
  13. // *** Create the FTP object
  14. $ftpObj = new FTPClient();
  15.  
  16.  
  17. // *** Connect
  18. if ($ftpObj -> connect(FTP_HOST, FTP_USER, FTP_PASS)) {
  19.  
  20.  
  21. ## --------------------------------------------------------
  22.  
  23. # Step 7
  24.  
  25. $dir = '/photos';
  26.  
  27. // *** Make directory
  28. $ftpObj->makeDir($dir);
  29.  
  30. print_r($ftpObj -> getMessages());
  31.  
  32. ## --------------------------------------------------------
  33.  
  34. # Step 8
  35.  
  36. $fileFrom = 'zoe.jpg';
  37. $fileTo = $dir . '/' . $fileFrom;
  38.  
  39. // *** Upload local file to new directory on server
  40. $ftpObj -> uploadFile($fileFrom, $fileTo);
  41.  
  42. print_r($ftpObj -> getMessages());
  43.  
  44. ## --------------------------------------------------------
  45.  
  46. # Step 9
  47.  
  48. // *** Change to folder
  49. $ftpObj->changeDir($dir);
  50.  
  51. // *** Get folder contents
  52. $contentsArray = $ftpObj->getDirListing();
  53.  
  54.  
  55. // *** Output our array of folder contents
  56. echo '<pre>';
  57. print_r($contentsArray);
  58. echo '</pre>';
  59.  
  60. ## --------------------------------------------------------
  61.  
  62. # Step 10
  63.  
  64. $fileFrom = 'zoe.jpg'; # The location on the server
  65. $fileTo = 'zoe-new.jpg'; # Local dir to save to
  66.  
  67. // *** Download file
  68. $ftpObj->downloadFile($fileFrom, $fileTo);
  69.  
  70. }
  71. ?>

  1. <?php
  2.  
  3. # Step 2
  4. Class FTPClient
  5. {
  6. // *** Class variables # Step 3
  7. private $connectionId;
  8. private $loginOk = false;
  9. private $messageArray = array();
  10.  
  11.  
  12. public function __construct() { }
  13.  
  14. ## --------------------------------------------------------
  15.  
  16. # Step 4
  17. private function logMessage($message, $clear=true)
  18. {
  19. if ($clear) {$this->messageArray = array();}
  20.  
  21. $this->messageArray[] = $message;
  22. }
  23.  
  24. ## --------------------------------------------------------
  25.  
  26. # Step 4
  27. public function getMessages()
  28. {
  29. return $this->messageArray;
  30. }
  31.  
  32. ## --------------------------------------------------------
  33.  
  34. # Step 5
  35. public function connect ($server, $ftpUser, $ftpPassword, $isPassive = true)
  36. {
  37.  
  38. // *** Set up basic connection
  39. $this->connectionId = ftp_connect($server);
  40.  
  41. // *** Login with username and password
  42. $loginResult = ftp_login($this->connectionId, $ftpUser, $ftpPassword);
  43.  
  44. // *** Sets passive mode on/off (default off)
  45. ftp_pasv($this->connectionId, $isPassive);
  46.  
  47. // *** Check connection
  48. if ((!$this->connectionId) || (!$loginResult)) {
  49. $this->logMessage('FTP connection has failed!');
  50. $this->logMessage('Attempted to connect to ' . $server . ' for user ' . $ftpUser, true);
  51. return false;
  52. } else {
  53. $this->logMessage('Connected to ' . $server . ', for user ' . $ftpUser);
  54. $this->loginOk = true;
  55. return true;
  56. }
  57. }
  58.  
  59. ## --------------------------------------------------------
  60.  
  61. # Step 7
  62. public function makeDir($directory)
  63. {
  64. // *** If creating a directory is successful...
  65. if (ftp_mkdir($this->connectionId, $directory)) {
  66.  
  67. $this->logMessage('Directory "' . $directory . '" created successfully');
  68. return true;
  69.  
  70. } else {
  71.  
  72. // *** ...Else, FAIL.
  73. $this->logMessage('Failed creating directory "' . $directory . '"');
  74. return false;
  75. }
  76. }
  77.  
  78. ## --------------------------------------------------------
  79.  
  80. # Step 8
  81. public function uploadFile ($fileFrom, $fileTo)
  82. {
  83. // *** Set the transfer mode
  84. $asciiArray = array('txt', 'csv');
  85. $extension = end(explode('.', $fileFrom));
  86. if (in_array($extension, $asciiArray)) {
  87. $mode = FTP_ASCII;
  88. } else {
  89. $mode = FTP_BINARY;
  90. }
  91.  
  92. // *** Upload the file
  93. $upload = ftp_put($this->connectionId, $fileTo, $fileFrom, $mode);
  94.  
  95. // *** Check upload status
  96. if (!$upload) {
  97.  
  98. $this->logMessage('FTP upload has failed!');
  99. return false;
  100.  
  101. } else {
  102. $this->logMessage('Uploaded "' . $fileFrom . '" as "' . $fileTo);
  103. return true;
  104. }
  105. }
  106.  
  107. ## --------------------------------------------------------
  108.  
  109. # Step 9
  110. public function changeDir($directory)
  111. {
  112. if (ftp_chdir($this->connectionId, $directory)) {
  113. $this->logMessage('Current directory is now: ' . ftp_pwd($this->connectionId));
  114. return true;
  115. } else {
  116. $this->logMessage('Couldn\'t change directory');
  117. return false;
  118. }
  119. }
  120.  
  121. ## --------------------------------------------------------
  122.  
  123. # Step 9
  124. public function getDirListing($directory = '.', $parameters = '-la')
  125. {
  126. // get contents of the current directory
  127. $contentsArray = ftp_nlist($this->connectionId, $parameters . ' ' . $directory);
  128.  
  129. return $contentsArray;
  130. }
  131.  
  132. ## --------------------------------------------------------
  133.  
  134. # Step 10
  135. public function downloadFile ($fileFrom, $fileTo)
  136. {
  137.  
  138. // *** Set the transfer mode
  139. $asciiArray = array('txt', 'csv');
  140. $extension = end(explode('.', $fileFrom));
  141. if (in_array($extension, $asciiArray)) {
  142. $mode = FTP_ASCII;
  143. } else {
  144. $mode = FTP_BINARY;
  145. }
  146.  
  147. // open some file to write to
  148. //$handle = fopen($fileTo, 'w');
  149.  
  150. // try to download $remote_file and save it to $handle
  151. if (ftp_get($this->connectionId, $fileTo, $fileFrom, $mode, 0)) {
  152.  
  153. return true;
  154. $this->logMessage(' file "' . $fileTo . '" successfully downloaded');
  155. } else {
  156.  
  157. return false;
  158. $this->logMessage('There was an error downloading file "' . $fileFrom . '" to "' . $fileTo . '"');
  159. }
  160.  
  161. }
  162.  
  163. ## --------------------------------------------------------
  164.  
  165. # Step 11
  166. public function __deconstruct()
  167. {
  168. if ($this->connectionId) {
  169. ftp_close($this->connectionId);
  170. }
  171. }
  172.  
  173. ## --------------------------------------------------------
  174.  
  175. }
  176.  
  177. ?>


Wynik tego skryptu:
  1. Warning: ftp_mkdir() [function.ftp-mkdir]: Can't create directory: File exists in C:\xampp\htdocs\ftp_class.php on line 65
  2. Array ( [0] => Failed creating directory "/photos" )
  3. Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\ftp_class.php on line 85
  4. Array ( [0] => Uploaded "zoe.jpg" as "/photos/zoe.jpg )
  5.  
  6. Array
  7. (
  8. )
  9.  
  10.  
  11. Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\ftp_class.php on line 140