国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 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*/ 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 迁西县| 勐海县| 田阳县| 金寨县| 隆回县| 杭锦后旗| 日照市| 云梦县| 丰城市| 新昌县| 沙湾县| 临城县| 博兴县| 德清县| 呼玛县| 金塔县| 航空| 宿松县| 宿州市| 安图县| 南通市| 芒康县| 开化县| 九寨沟县| 中牟县| 丽江市| 德昌县| 禄丰县| 黄冈市| 古田县| 揭西县| 永川市| 遵义县| 新建县| 沿河| 南木林县| 西青区| 吉林省| 三明市| 平凉市| 五常市|