亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > PHP > 正文

php實現通過ftp上傳文件

2024-05-04 23:36:33
字體:
來源:轉載
供稿:網友

在php中我們可以利用ftp_connect相關函數實現文件上傳與下載功能,其實就是ftp客戶端一樣的操作,下面我來給大家介紹如何利用php來實現

大概原理

遍歷項目中的所有非排除文件,然后獲取 文件修改時間晚于文件上一次修改時間 的文件

然后將這些文件,通過ftp上傳到對應的目錄

具體代碼如下:

因為只是工具,代碼很亂,見諒

 

 
  1. <?php 
  2. error_reporting(7); 
  3. if ($_SERVER['SERVER_ADDR'])exit;//禁止在web服務器下運行 
  4. $_GET['exclude'] = array('number.txt','uploads','Zend','docs','cache','You','managesdk'); //排除上傳目錄,定義為全局變量 
  5. $fileobj = new FilerFile(); 
  6. $path = "/data/longtu/"//項目目錄的根目錄 
  7. $files = $fileobj->Zip($path); //過濾出最新的修改文件 
  8. $path = str_replace("/data/longtu/","",$path); 
  9. $config = array
  10. 'hostname' => 'xxx.xxx.xx.xxx'//ftp服務器 地址 
  11. 'username' => 'xxx'//ftp用戶 
  12. 'password' => '?xxxxxxxxxxx'//ftp密碼 
  13. 'port' => 21 //端口 
  14. ); 
  15. $ftp = new Ftp(); 
  16. $ftp->connect($config); //鏈接服務器 
  17. //$a=$ftp->filelist(); 
  18. $LOCAL_ROOT = realpath(dirname(__DIR__)."/../../"); 
  19. chdir($LOCAL_ROOT); 
  20. foreach ($files as $k=>$v){ 
  21. $f = $path.$v
  22. $tmp = $ftp->upload($f$f); 
  23. if($tmp){ 
  24. echo "upload $f -> success /n"
  25. //$ftp->download('ftp_upload.log','ftp_download.log'); 
  26. // 
  27. //$ftp->upload('ftp_err.log','ftp_upload.log'); 
  28. //$ftp->download('ftp_upload.log','ftp_download.log'); 
  29. /* 
  30. * 
  31. * 
  32. * $dir = "/test"; 
  33. if(@ftp_chdir($conn, $dir)) 
  34. 判斷是否為文件夾 
  35. * Enter description here ... 
  36. * @author Administrator 
  37. * 
  38. */ 
  39. class FilerFile 
  40. var $time_path
  41. private $fctimes = array(); 
  42. function Zip($dir
  43. $this->time_path = rtrim($dir,"/")."/.~~~time.php"
  44. //@unlink($this->time_path); 
  45. $filelist = $this -> GetFileList($dir); 
  46. file_put_contents($this->time_path,"<?php /n return ".var_export($this->fctimes,true).";"); 
  47. return $filelist
  48. function appendFilectime($file
  49. $time_file_path = $this->time_path; 
  50. $ftime = @include($time_file_path); 
  51. $ftime = $ftime ? $ftime : array(); 
  52. $time = filectime($file); 
  53. if(!file_exists($time_file_path))file_put_contents($time_file_path,"<?php /n"); 
  54. function getFileByFilectime($file
  55. static $time_data ; 
  56. $time_file_path = $this->time_path; 
  57. if (!$time_data){ 
  58. $time_data= @include_once($time_file_path); 
  59. $time_data = $time_data ? $time_data : array(); 
  60. //var_dump($file,$time_data[$file] == filectime($file)); 
  61. //echo $file."/t".$time_data[$file]."/n"; 
  62. if ($time_data[$file] == filemtime($file)){ 
  63. return false; 
  64. }else
  65. return $file
  66. function GetFileList($dir,$path=""
  67. static $tmpp = ""
  68. if ($path=="" && !$tmpp){ 
  69. $tmpp = $dir
  70. $d = dir($dir); 
  71. $files = array(); 
  72. if ($d
  73. $pathP=str_replace($tmpp,"",$dir); 
  74. $pathP=str_replace(array("////","/"),DIRECTORY_SEPARATOR,$pathP); 
  75. $pathP=str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$pathP); 
  76. while($f = $d->read()) 
  77. if ($f == '.' || $f=='..' || $f{0}=='.' || $f=='Zend' || in_array($f$_GET['exclude']))continue
  78. $newdir = rtrim($dir,"/")."/".$f
  79. if (is_dir($newdir)){ 
  80. $files = array_merge($files,$this->GetFileList($newdir,$newdir)); 
  81. }else
  82. $abspath_file = (rtrim($dir,"/") ? rtrim($dir,"/")."/" : "").$f
  83. $this->fctimes[$abspath_file] = filemtime($abspath_file); 
  84. if (!$this->getFileByFilectime($abspath_file))continue
  85. $file = (rtrim($pathP,"/") ? rtrim($pathP,"/")."/" : "").$f
  86. $files[] = $file
  87. return $files
  88. /** 
  89. * 仿寫CodeIgniter的FTP類 
  90. * FTP基本操作: 
  91. * 1) 登陸; connect 
  92. * 2) 當前目錄文件列表; filelist 
  93. * 3) 目錄改變; chgdir 
  94. * 4) 重命名/移動; rename 
  95. * 5) 創建文件夾; mkdir 
  96. * 6) 刪除; delete_dir/delete_file 
  97. * 7) 上傳; upload 
  98. * 8) 下載 download 
  99. * 
  100. * @author quanshuidingdang 
  101. */ 
  102. class Ftp { 
  103. private $hostname = ''
  104. private $username = ''
  105. private $password = ''
  106. private $port = 21; 
  107. private $passive = TRUE; 
  108. private $debug = TRUE; 
  109. private $conn_id = FALSE; 
  110. /** 
  111. * 構造函數 
  112. * 
  113. * @param array 配置數組 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...); 
  114. */ 
  115. public function __construct($config = array()) { 
  116. if(count($config) > 0) { 
  117. $this->_init($config); 
  118. /** 
  119. * FTP連接 
  120. * 
  121. * @access public 
  122. * @param array 配置數組 
  123. * @return boolean 
  124. */ 
  125. public function connect($config = array()) { 
  126. if(count($config) > 0) { 
  127. $this->_init($config); 
  128. if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) { 
  129. if($this->debug === TRUE) { 
  130. $this->_error("ftp_unable_to_connect"); 
  131. return FALSE; 
  132. if( ! $this->_login()) { 
  133. if($this->debug === TRUE) { 
  134. $this->_error("ftp_unable_to_login"); 
  135. return FALSE; 
  136. if($this->passive === TRUE) { 
  137. ftp_pasv($this->conn_id, TRUE); 
  138. return TRUE; 
  139. /** 
  140. * 文件夾是否存在 
  141. * @param unknown_type $path 
  142. */ 
  143. public function is_dir_exists($path
  144. return $this->chgdir($path); 
  145. /** 
  146. * 目錄改變 
  147. * 
  148. * @access public 
  149. * @param string 目錄標識(ftp) 
  150. * @param boolean  
  151. * @return boolean 
  152. */ 
  153. public function chgdir($path = ''$supress_debug = FALSE) { 
  154. if($path == '' OR ! $this->_isconn()) { 
  155. return FALSE; 
  156. $result = @ftp_chdir($this->conn_id, $path); 
  157. if($result === FALSE) { 
  158. if($this->debug === TRUE AND $supress_debug == FALSE) { 
  159. $this->_error("ftp_unable_to_chgdir:dir[".$path."]"); 
  160. return FALSE; 
  161. return TRUE; 
  162. /** 
  163. * 目錄生成 
  164. * 
  165. * @access public 
  166. * @param string 目錄標識(ftp) 
  167. * @param int 文件權限列表  
  168. * @return boolean 
  169. */ 
  170. public function mkdir($path = ''$permissions = NULL) { 
  171. if($path == '' OR ! $this->_isconn()) { 
  172. return FALSE; 
  173. $result = @ftp_mkdir($this->conn_id, $path); 
  174. if($result === FALSE) { 
  175. if($this->debug === TRUE) { 
  176. $this->_error("ftp_unable_to_mkdir:dir[".$path."]"); 
  177. return FALSE; 
  178. if( ! is_null($permissions)) { 
  179. $this->chmod($path,(int)$permissions); 
  180. return TRUE; 
  181. /** 
  182. * 上傳 
  183. * 
  184. * @access public 
  185. * @param string 本地目錄標識 
  186. * @param string 遠程目錄標識(ftp) 
  187. * @param string 上傳模式 auto || ascii 
  188. * @param int 上傳后的文件權限列表  
  189. * @return boolean 
  190. */ 
  191. public function upload($localpath$remotepath$mode = 'auto'$permissions = NULL) { 
  192. if( ! $this->_isconn()) { 
  193. return FALSE; 
  194. if( ! file_exists($localpath)) { 
  195. if($this->debug === TRUE) { 
  196. $this->_error("ftp_no_source_file:".$localpath); 
  197. return FALSE; 
  198. if($mode == 'auto') { 
  199. $ext = $this->_getext($localpath); 
  200. $mode = $this->_settype($ext); 
  201. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY; 
  202. $result = @ftp_put($this->conn_id, $remotepath$localpath$mode); 
  203. if($result === FALSE) { 
  204. if($this->debug === TRUE) { 
  205. $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]"); 
  206. return FALSE; 
  207. if( ! is_null($permissions)) { 
  208. $this->chmod($remotepath,(int)$permissions); 
  209. return TRUE; 
  210. /** 
  211. * 下載 
  212. * 
  213. * @access public 
  214. * @param string 遠程目錄標識(ftp) 
  215. * @param string 本地目錄標識 
  216. * @param string 下載模式 auto || ascii  
  217. * @return boolean 
  218. */ 
  219. public function download($remotepath$localpath$mode = 'auto') { 
  220. if( ! $this->_isconn()) { 
  221. return FALSE; 
  222. if($mode == 'auto') { 
  223. $ext = $this->_getext($remotepath); 
  224. $mode = $this->_settype($ext); 
  225. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY; 
  226. $result = @ftp_get($this->conn_id, $localpath$remotepath$mode); 
  227. if($result === FALSE) { 
  228. if($this->debug === TRUE) { 
  229. $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]"); 
  230. return FALSE; 
  231. return TRUE; 
  232. /** 
  233. * 重命名/移動 
  234. * 
  235. * @access public 
  236. * @param string 遠程目錄標識(ftp) 
  237. * @param string 新目錄標識 
  238. * @param boolean 判斷是重命名(FALSE)還是移動(TRUE)  
  239. * @return boolean 
  240. */ 
  241. public function rename($oldname$newname$move = FALSE) { 
  242. if( ! $this->_isconn()) { 
  243. return FALSE; 
  244. $result = @ftp_rename($this->conn_id, $oldname$newname); 
  245. if($result === FALSE) { 
  246. if($this->debug === TRUE) { 
  247. $msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move"
  248. $this->_error($msg); 
  249. return FALSE; 
  250. return TRUE; 
  251. /** 
  252. * 刪除文件 
  253. * 
  254. * @access public 
  255. * @param string 文件標識(ftp) 
  256. * @return boolean 
  257. */ 
  258. public function delete_file($file) { 
  259. if( ! $this->_isconn()) { 
  260. return FALSE; 
  261. $result = @ftp_delete($this->conn_id, $file); 
  262. if($result === FALSE) { 
  263. if($this->debug === TRUE) { 
  264. $this->_error("ftp_unable_to_delete_file:file[".$file."]"); 
  265. return FALSE; 
  266. return TRUE; 
  267. /** 
  268. * 刪除文件夾 
  269. * 
  270. * @access public 
  271. * @param string 目錄標識(ftp) 
  272. * @return boolean 
  273. */ 
  274. public function delete_dir($path) { 
  275. if( ! $this->_isconn()) { 
  276. return FALSE; 
  277. //對目錄宏的'/'字符添加反斜杠'/' 
  278. $path = preg_replace("/(.+?)//*$/""//1/"$path); 
  279. //獲取目錄文件列表 
  280. $filelist = $this->filelist($path); 
  281. if($filelist !== FALSE AND count($filelist) > 0) { 
  282. foreach($filelist as $item) { 
  283. //如果我們無法刪除,那么就可能是一個文件夾 
  284. //所以我們遞歸調用delete_dir() 
  285. if( ! @delete_file($item)) { 
  286. $this->delete_dir($item); 
  287. //刪除文件夾(空文件夾) 
  288. $result = @ftp_rmdir($this->conn_id, $path); 
  289. if($result === FALSE) { 
  290. if($this->debug === TRUE) { 
  291. $this->_error("ftp_unable_to_delete_dir:dir[".$path."]"); 
  292. return FALSE; 
  293. return TRUE; 
  294. /** 
  295. * 修改文件權限 
  296. * @access public 
  297. * @param string 目錄標識(ftp) 
  298. * @return boolean 
  299. */ 
  300. public function chmod($path$perm) { 
  301. if( ! $this->_isconn()) { 
  302. return FALSE; 
  303. //只有在PHP5中才定義了修改權限的函數(ftp) 
  304. if( ! function_exists('ftp_chmod')) { 
  305. if($this->debug === TRUE) { 
  306. $this->_error("ftp_unable_to_chmod(function)"); 
  307. return FALSE; 
  308. $result = @ftp_chmod($this->conn_id, $perm$path); 
  309. if($result === FALSE) { 
  310. if($this->debug === TRUE) { 
  311. $this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]"); 
  312. return FALSE; 
  313. return TRUE; 
  314. /** 
  315. * 獲取目錄文件列表 
  316. * 
  317. * @access public 
  318. * @param string 目錄標識(ftp) 
  319. * @return array 
  320. */ 
  321. public function filelist($path = '.') { 
  322. if( ! $this->_isconn()) { 
  323. return FALSE; 
  324. return ftp_nlist($this->conn_id, $path); 
  325. /** 
  326. * 關閉FTP 
  327. * 
  328. * @access public 
  329. * @return boolean 
  330. */ 
  331. public function close() { 
  332. if( ! $this->_isconn()) { 
  333. return FALSE; 
  334. return @ftp_close($this->conn_id); 
  335. /** 
  336. * FTP成員變量初始化 
  337. * 
  338. * @access private 
  339. * @param array 配置數組  
  340. * @return void 
  341. */ 
  342. private function _init($config = array()) { 
  343. foreach($config as $key => $val) { 
  344. if(isset($this->$key)) { 
  345. $this->$key = $val
  346. //特殊字符過濾 
  347. $this->hostname = preg_replace('|.+?://|','',$this->hostname); 
  348. /** 
  349. * FTP登陸 
  350. * 
  351. * @access private 
  352. * @return boolean 
  353. */ 
  354. private function _login() { 
  355. return @ftp_login($this->conn_id, $this->username, $this->password); 
  356. /** 
  357. * 判斷con_id 
  358. * 
  359. * @access private 
  360. * @return boolean 
  361. */ 
  362. private function _isconn() { 
  363. if( ! is_resource($this->conn_id)) { 
  364. if($this->debug === TRUE) { 
  365. $this->_error("ftp_no_connection"); 
  366. return FALSE; 
  367. return TRUE; 
  368. /** 
  369. * 從文件名中獲取后綴擴展 
  370. * 
  371. * @access private 
  372. * @param string 目錄標識 
  373. * @return string 
  374. */ 
  375. private function _getext($filename) { 
  376. if(FALSE === strpos($filename'.')) { 
  377. return 'txt'
  378. $extarr = explode('.'$filename); 
  379. return end($extarr); 
  380. /** 
  381. * 從后綴擴展定義FTP傳輸模式 ascii 或 binary 
  382. * 
  383. * @access private 
  384. * @param string 后綴擴展 
  385. * @return string 
  386. */ 
  387. private function _settype($ext) { 
  388. $text_type = array ( 
  389. 'txt'
  390. 'text'
  391. 'php'
  392. 'phps'
  393. 'php4'
  394. 'js'
  395. 'css'
  396. 'htm'
  397. 'html'
  398. 'phtml'
  399. 'shtml'
  400. 'log'
  401. 'xml' 
  402. ); 
  403. return (in_array($ext$text_type)) ? 'ascii' : 'binary'
  404. /** 
  405. * 錯誤日志記錄 
  406. * 
  407. * @access prvate 
  408. * @return boolean 
  409. */ 
  410. private function _error($msg) { 
  411. return @file_put_contents('/tmp/ftp_err.log'"date[".date("Y-m-d H:i:s")."]-hostname[".$this->hostname."]-username[".$this->username."]-password[".$this->password."]-msg[".$msg."]/n", FILE_APPEND); 
  412. /*End of file ftp.php*/ 
  413. /*Location /Apache Group/htdocs/ftp.php*/ 

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品最新在线观看| 成人免费网视频| 在线视频欧美日韩| 欧美极品少妇xxxxⅹ喷水| 亚洲精品aⅴ中文字幕乱码| 亚洲激情 国产| 亚洲人成欧美中文字幕| 欧亚精品中文字幕| 91夜夜未满十八勿入爽爽影院| 九九久久久久久久久激情| 97香蕉久久夜色精品国产| 日韩毛片中文字幕| 久久69精品久久久久久久电影好| 午夜精品久久久久久99热| 国产精品免费观看在线| 国产精品自拍偷拍视频| 国产精品成人久久久久| 国产成人精品免高潮在线观看| 色综合久久中文字幕综合网小说| 国产精品对白刺激| 疯狂做受xxxx高潮欧美日本| 一夜七次郎国产精品亚洲| 国产xxx69麻豆国语对白| 78m国产成人精品视频| 91高潮精品免费porn| 成人性生交xxxxx网站| 日韩美女视频在线观看| 57pao国产成人免费| 亚洲二区在线播放视频| 日韩亚洲欧美中文高清在线| 久久久久久久国产精品视频| 午夜精品蜜臀一区二区三区免费| 日韩中文字幕网| 欧美激情视频网| 亚洲午夜未满十八勿入免费观看全集| 欧美日韩亚洲视频一区| 国产z一区二区三区| 亚洲成人免费在线视频| 午夜精品久久久久久久99热| 欧美日韩国产精品一区| 中文字幕精品一区二区精品| 日韩美女av在线| 精品国产视频在线| 亚洲免费伊人电影在线观看av| 欧美性猛交xxxx免费看| 欧美日本亚洲视频| 日韩av中文字幕在线播放| 亚洲一区二区中文字幕| 久久久久久国产精品久久| 国产精品成人国产乱一区| 久久久免费观看| 亚洲一级片在线看| 91亚洲精品久久久久久久久久久久| 国产又爽又黄的激情精品视频| 久久久久久成人| 国产精品自产拍高潮在线观看| 亚洲跨种族黑人xxx| 国产日韩亚洲欧美| 国产精品专区一| 亚洲国产成人在线播放| 国产精品精品久久久| 国产精品第1页| 日韩欧美在线视频日韩欧美在线视频| 久久6免费高清热精品| 欧美日韩福利在线观看| 日韩美女毛茸茸| 成人午夜在线影院| 在线成人激情黄色| 国产丝袜一区二区三区免费视频| 亚洲欧洲国产一区| 亚洲日本欧美中文幕| 国产精品视频导航| 日韩在线欧美在线国产在线| 在线观看日韩欧美| 国产精品美女久久久久久免费| 色婷婷综合久久久久中文字幕1| 国产一区二区在线播放| 欧美日韩在线视频首页| 日韩av网站电影| 日本精品性网站在线观看| 最近2019年日本中文免费字幕| 1769国产精品| 国产精品美女久久久久久免费| 亚洲综合成人婷婷小说| 日韩av不卡电影| 国产精品欧美激情| 黑人巨大精品欧美一区二区三区| 国产精品扒开腿做爽爽爽男男| 欧美一区视频在线| 亚洲精品美女在线| 国产99久久精品一区二区永久免费| 亚洲大胆人体在线| 青青草成人在线| 国产在线拍偷自揄拍精品| 国产精品成人观看视频国产奇米| 国产欧美一区二区三区久久人妖| 亚洲最大福利视频网| 欧美一区二区.| 亚洲性生活视频| 亚洲国产免费av| 国产成人久久久精品一区| 亚洲欧美国产精品va在线观看| 懂色av影视一区二区三区| 亚洲一区二区少妇| 26uuu另类亚洲欧美日本一| 欧美性xxxx极品hd满灌| 欧美性猛交xxxx乱大交3| 欧美亚洲日本网站| 日韩专区在线播放| 欧美成人午夜激情在线| 久久精品中文字幕| 精品久久久久久久久久久久久| 久久精品一偷一偷国产| 精品国产91久久久久久老师| 日韩中文第一页| 日本a级片电影一区二区| 日韩中文视频免费在线观看| 91精品久久久久久久久久入口| 亚洲欧洲自拍偷拍| 一区二区三区 在线观看视| 亚洲美女激情视频| 91久热免费在线视频| 成人激情视频小说免费下载| 欧美国产高跟鞋裸体秀xxxhd| 91国产高清在线| 亚洲一区二区三区毛片| 91精品国产高清久久久久久久久| 欧美黄色小视频| 国产欧美中文字幕| 91九色国产视频| 日韩av一区在线观看| 欧美精品情趣视频| 国产成人啪精品视频免费网| 久久精品国产成人| 国模视频一区二区| 久久在线观看视频| 91豆花精品一区| 日韩欧美中文字幕在线观看| 在线一区二区日韩| 粗暴蹂躏中文一区二区三区| 久久91精品国产91久久跳| 国产偷亚洲偷欧美偷精品| 国产成人极品视频| 欧美电影《睫毛膏》| 亚洲男人天堂2024| 亚洲最新av在线网站| 欧美大肥婆大肥bbbbb| 欧美在线影院在线视频| 成人免费午夜电影| 欧美精品videossex性护士| 欧美激情视频一区| 欧美最顶级的aⅴ艳星| 成人亲热视频网站| 青草热久免费精品视频| 91av在线视频观看| 日韩色av导航| 国产综合在线观看视频| 麻豆一区二区在线观看| 国产精品网址在线| 久久精品国产免费观看| 国产精品视频白浆免费视频| 久久免费国产精品1| 国产精品成人国产乱一区| 日韩中文字幕在线看|