FTP.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. namespace ZN\Services;
  3. class __USE_STATIC_ACCESS__FTP implements FTPInterface
  4. {
  5. //----------------------------------------------------------------------------------------------------
  6. //
  7. // Yazar : Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
  8. // Site : www.zntr.net
  9. // Lisans : The MIT License
  10. // Telif Hakkı: Copyright (c) 2012-2016, zntr.net
  11. //
  12. //----------------------------------------------------------------------------------------------------
  13. //----------------------------------------------------------------------------------------------------
  14. // Const CONFIG_NAME
  15. //----------------------------------------------------------------------------------------------------
  16. //
  17. // @const string
  18. //
  19. //----------------------------------------------------------------------------------------------------
  20. const CONFIG_NAME = 'Services:ftp';
  21. //----------------------------------------------------------------------------------------------------
  22. // Protected $connect
  23. //----------------------------------------------------------------------------------------------------
  24. //
  25. // @const resource
  26. //
  27. //----------------------------------------------------------------------------------------------------
  28. protected $connect = NULL;
  29. //----------------------------------------------------------------------------------------------------
  30. // Protected $login
  31. //----------------------------------------------------------------------------------------------------
  32. //
  33. // @const resource
  34. //
  35. //----------------------------------------------------------------------------------------------------
  36. protected $login = NULL;
  37. //----------------------------------------------------------------------------------------------------
  38. // __construct()
  39. //----------------------------------------------------------------------------------------------------
  40. //
  41. // @param array $config: empty
  42. //
  43. //----------------------------------------------------------------------------------------------------
  44. public function __construct($config = [])
  45. {
  46. $this->config($config);
  47. $this->connect();
  48. }
  49. //----------------------------------------------------------------------------------------------------
  50. // Call Undefined Method
  51. //----------------------------------------------------------------------------------------------------
  52. //
  53. // __call()
  54. //
  55. //----------------------------------------------------------------------------------------------------
  56. use \CallUndefinedMethodTrait;
  57. //----------------------------------------------------------------------------------------------------
  58. // Config Method
  59. //----------------------------------------------------------------------------------------------------
  60. //
  61. // config()
  62. //
  63. //----------------------------------------------------------------------------------------------------
  64. use \ConfigMethodTrait;
  65. //----------------------------------------------------------------------------------------------------
  66. // Error Control
  67. //----------------------------------------------------------------------------------------------------
  68. //
  69. // $error
  70. // $success
  71. //
  72. // error()
  73. // success()
  74. //
  75. //----------------------------------------------------------------------------------------------------
  76. use \ErrorControlTrait;
  77. //----------------------------------------------------------------------------------------------------
  78. // connect()
  79. //----------------------------------------------------------------------------------------------------
  80. //
  81. // @param array $config: empty
  82. //
  83. //----------------------------------------------------------------------------------------------------
  84. public function connect($config = [])
  85. {
  86. if( ! is_array($config) )
  87. {
  88. return \Errors::set('Error', 'arrayParameter', 'config');
  89. }
  90. if( ! empty($config) )
  91. {
  92. $this->config($config);
  93. }
  94. // Config/Ftp.php dosyasından ftp ayarları alınıyor.
  95. $config = $this->config;
  96. // ----------------------------------------------------------------------------
  97. // FTP BAĞLANTI AYARLARI YAPILANDIRILIYOR
  98. // ----------------------------------------------------------------------------
  99. $host = $config['host'];
  100. $port = $config['port'];
  101. $timeout = $config['timeout'];
  102. $user = $config['user'];
  103. $password = $config['password'];
  104. $ssl = $config['sslConnect'];
  105. // ----------------------------------------------------------------------------
  106. // Bağlantı türü ayarına göre ssl veya normal
  107. // bağlatı yapılıp yapılmayacağı belirlenir.
  108. $this->connect = ( $ssl === false )
  109. ? @ftp_connect($host, $port, $timeout)
  110. : @ftp_ssl_connect($host, $port, $timeout);
  111. if( empty($this->connect) )
  112. {
  113. return \Errors::set('Error', 'emptyVariable', '@this->connect');
  114. }
  115. $this->login = @ftp_login($this->connect, $user, $password);
  116. if( empty($this->login) )
  117. {
  118. return false;
  119. }
  120. return $this;
  121. }
  122. //----------------------------------------------------------------------------------------------------
  123. // close()
  124. //----------------------------------------------------------------------------------------------------
  125. //
  126. // @param void
  127. //
  128. //----------------------------------------------------------------------------------------------------
  129. public function close()
  130. {
  131. if( ! empty($this->connect) )
  132. {
  133. @ftp_close($this->connect);
  134. }
  135. }
  136. //----------------------------------------------------------------------------------------------------
  137. // createFolder()
  138. //----------------------------------------------------------------------------------------------------
  139. //
  140. // @param string $path: empty
  141. //
  142. //----------------------------------------------------------------------------------------------------
  143. public function createFolder($path = '')
  144. {
  145. if( ! is_string($path) )
  146. {
  147. return \Errors::set('Error', 'stringParameter', 'path');
  148. }
  149. if( @ftp_mkdir($this->connect, $path) )
  150. {
  151. return true;
  152. }
  153. else
  154. {
  155. return \Errors::set('Folder', 'alreadyFileError', $path);
  156. }
  157. }
  158. //----------------------------------------------------------------------------------------------------
  159. // deleteFolder()
  160. //----------------------------------------------------------------------------------------------------
  161. //
  162. // @param string $path: empty
  163. //
  164. //----------------------------------------------------------------------------------------------------
  165. public function deleteFolder($path = '')
  166. {
  167. if( ! is_string($path) )
  168. {
  169. return \Errors::set('Error', 'stringParameter', 'path');
  170. }
  171. if( @ftp_rmdir($this->connect, $path) )
  172. {
  173. return true;
  174. }
  175. else
  176. {
  177. return \Errors::set('Folder', 'notFoundError', $path);
  178. }
  179. }
  180. //----------------------------------------------------------------------------------------------------
  181. // changeFolder()
  182. //----------------------------------------------------------------------------------------------------
  183. //
  184. // @param string $path: empty
  185. //
  186. //----------------------------------------------------------------------------------------------------
  187. public function changeFolder($path = '')
  188. {
  189. if( ! is_string($path) )
  190. {
  191. return \Errors::set('Error', 'stringParameter', 'path');
  192. }
  193. if( @ftp_chdir($this->connect, $path) )
  194. {
  195. return true;
  196. }
  197. else
  198. {
  199. return \Errors::set('Folder', 'changeFolderError', $path);
  200. }
  201. }
  202. //----------------------------------------------------------------------------------------------------
  203. // rename()
  204. //----------------------------------------------------------------------------------------------------
  205. //
  206. // @param string $oldName: empty
  207. // @param string $newName: empty
  208. //
  209. //----------------------------------------------------------------------------------------------------
  210. public function rename($oldName = '', $newName = '')
  211. {
  212. if( ! is_string($oldName) || ! is_string($newName) )
  213. {
  214. \Errors::set('Error', 'stringParameter', 'oldName');
  215. \Errors::set('Error', 'stringParameter', 'newName');
  216. return false;
  217. }
  218. if( @ftp_rename($this->connect, $oldName, $newName) )
  219. {
  220. return true;
  221. }
  222. else
  223. {
  224. return \Errors::set('Folder', 'changeFolderNameError', $oldName);
  225. }
  226. }
  227. //----------------------------------------------------------------------------------------------------
  228. // deleteFile()
  229. //----------------------------------------------------------------------------------------------------
  230. //
  231. // @param string $path: empty
  232. //
  233. //----------------------------------------------------------------------------------------------------
  234. public function deleteFile($path = '')
  235. {
  236. if( ! is_string($path) )
  237. {
  238. return \Errors::set('Error', 'stringParameter', 'path');
  239. }
  240. if( @ftp_delete($this->connect, $path) )
  241. {
  242. return true;
  243. }
  244. else
  245. {
  246. return \Errors::set('File', 'notFoundError', $path);
  247. }
  248. }
  249. //----------------------------------------------------------------------------------------------------
  250. // upload()
  251. //----------------------------------------------------------------------------------------------------
  252. //
  253. // @param string $localPath : empty
  254. // @param string $remotePath: empty
  255. // @param string $type : binary, ascii
  256. //
  257. //----------------------------------------------------------------------------------------------------
  258. public function upload($localPath = '', $remotePath = '', $type = 'ascii')
  259. {
  260. if( ! is_string($localPath) || ! is_string($remotePath) )
  261. {
  262. \Errors::set('Error', 'stringParameter', 'localPath');
  263. \Errors::set('Error', 'stringParameter', 'remotePath');
  264. return false;
  265. }
  266. if( ! is_string($type) )
  267. {
  268. $type = 'ascii';
  269. }
  270. if( @ftp_put($this->connect, $remotePath, $localPath, \Convert::toConstant($type, 'FTP_')) )
  271. {
  272. return true;
  273. }
  274. else
  275. {
  276. return \Errors::set('File', 'remoteUploadError', $localPath);
  277. }
  278. }
  279. //----------------------------------------------------------------------------------------------------
  280. // dowload()
  281. //----------------------------------------------------------------------------------------------------
  282. //
  283. // @param string $remotePath: empty
  284. // @param string $localPath : empty
  285. // @param string $type : binary, ascii
  286. //
  287. //----------------------------------------------------------------------------------------------------
  288. public function download($remotePath = '', $localPath = '', $type = 'ascii')
  289. {
  290. if( ! is_string($localPath) || ! is_string($remotePath) )
  291. {
  292. \Errors::set('Error', 'stringParameter', 'remotePath');
  293. \Errors::set('Error', 'stringParameter', 'localPath');
  294. return false;
  295. }
  296. if( ! is_string($type) )
  297. {
  298. $type = 'ascii';
  299. }
  300. if( @ftp_get($this->connect, $localPath, $remotePath, \Convert::toConstant($type, 'FTP_')) )
  301. {
  302. return true;
  303. }
  304. else
  305. {
  306. return \Errors::set('File', 'remoteDownloadError', $localPath);
  307. }
  308. }
  309. //----------------------------------------------------------------------------------------------------
  310. // permission()
  311. //----------------------------------------------------------------------------------------------------
  312. //
  313. // @param string $path: empty
  314. // @param int $type : 0755
  315. //
  316. //----------------------------------------------------------------------------------------------------
  317. public function permission($path = '', $type = 0755)
  318. {
  319. if( ! is_string($path) )
  320. {
  321. return \Errors::set('Error', 'stringParameter', 'path');
  322. }
  323. if( ! is_numeric($type) )
  324. {
  325. $type = 0755;
  326. }
  327. if( @ftp_chmod($this->connect, $type, $path) )
  328. {
  329. return true;
  330. }
  331. else
  332. {
  333. return \Errors::set('Error', 'emptyVariable', '@this->connect');
  334. }
  335. }
  336. //----------------------------------------------------------------------------------------------------
  337. // files()
  338. //----------------------------------------------------------------------------------------------------
  339. //
  340. // @param string $path : empty
  341. // @param string $extension: empty
  342. //
  343. //----------------------------------------------------------------------------------------------------
  344. public function files($path = '', $extension = '')
  345. {
  346. if( ! is_string($path) )
  347. {
  348. return \Errors::set('Error', 'stringParameter', 'path');
  349. }
  350. $list = @ftp_nlist($this->connect, $path);
  351. if( ! empty($list) ) foreach( $list as $file )
  352. {
  353. if( $file !== '.' && $file !== '..' )
  354. {
  355. if( ! empty($extension) && $extension !== 'dir' )
  356. {
  357. if( extension($file) === $extension )
  358. {
  359. $files[] = $file;
  360. }
  361. }
  362. else
  363. {
  364. if( $extension === 'dir' )
  365. {
  366. $extens = extension($file);
  367. if( empty($extens) )
  368. {
  369. $files[] = $file;
  370. }
  371. }
  372. else
  373. {
  374. $files[] = $file;
  375. }
  376. }
  377. }
  378. }
  379. if( ! empty($files) )
  380. {
  381. return $files;
  382. }
  383. else
  384. {
  385. return \Errors::set('Error', 'emptyVariable', '@files');
  386. }
  387. }
  388. //----------------------------------------------------------------------------------------------------
  389. // fileSize()
  390. //----------------------------------------------------------------------------------------------------
  391. //
  392. // @param string $path : empty
  393. // @param string $type : b, kb, mb, gb
  394. // @param int $decimal: 2
  395. //
  396. //----------------------------------------------------------------------------------------------------
  397. public function fileSize($path = '', $type = 'b', $decimal = 2)
  398. {
  399. if( ! is_string($path) )
  400. {
  401. return \Errors::set('Error', 'stringParameter', 'path');
  402. }
  403. if( ! is_string($type) )
  404. {
  405. $type = 'b';
  406. }
  407. $size = 0;
  408. $extension = extension($path);
  409. if( ! empty($extension) )
  410. {
  411. $size = @ftp_size($this->connect, $path);
  412. }
  413. else
  414. {
  415. if( $this->files($path) )
  416. {
  417. foreach($this->files($path) as $val)
  418. {
  419. $size += @ftp_size($this->connect, $path."/".$val);
  420. }
  421. $size += @ftp_size($this->connect, $path);
  422. }
  423. else
  424. {
  425. $size += @ftp_size($this->connect, $path);
  426. }
  427. }
  428. if( $type === "b" )
  429. {
  430. return $size;
  431. }
  432. if( $type === "kb" )
  433. {
  434. return round($size / 1024, $decimal);
  435. }
  436. if( $type === "mb" )
  437. {
  438. return round($size / (1024 * 1024), $decimal);
  439. }
  440. if( $type === "gb" )
  441. {
  442. return round($size / (1024 * 1024 * 1024), $decimal);
  443. }
  444. }
  445. //----------------------------------------------------------------------------------------------------
  446. // __destruct()
  447. //----------------------------------------------------------------------------------------------------
  448. //
  449. // @param void
  450. //
  451. //----------------------------------------------------------------------------------------------------
  452. public function __destruct()
  453. {
  454. $this->close();
  455. }
  456. }