首页 后端开发 php教程 ftp上传文件的php脚本

ftp上传文件的php脚本

Jul 25, 2016 am 08:42 AM

大概原理 遍历项目中的所有非排除文件,然后获取 文件修改时间晚于文件上一次修改时间 的文件 然后将这些文件,通过ftp上传到对应的目录 具体代码如下:

因为只是工具,代码很乱,见谅

  1. error_reporting(7);
  2. if ($_SERVER['SERVER_ADDR'])exit;//禁止在web服务器下运行
  3. $_GET['exclude'] = array('number.txt','uploads','Zend','docs','cache','You','managesdk'); //排除上传目录,定义为全局变量
  4. $fileobj = new FilerFile();
  5. $path = "/data/longtu/"; //项目目录的根目录
  6. $files = $fileobj->Zip($path); //过滤出最新的修改文件
  7. $path = str_replace("/data/longtu/","",$path);
  8. $config = array(
  9. 'hostname' => 'xxx.xxx.xx.xxx', //ftp服务器 地址
  10. 'username' => 'xxx', //ftp用户
  11. 'password' => '?xxxxxxxxxxx', //ftp密码
  12. 'port' => 21 //端口
  13. );
  14. $ftp = new Ftp();
  15. $ftp->connect($config); //链接服务器
  16. //$a=$ftp->filelist();
  17. $LOCAL_ROOT = realpath(dirname(__DIR__)."/../../");
  18. chdir($LOCAL_ROOT);
  19. foreach ($files as $k=>$v){
  20. $f = $path.$v;
  21. $tmp = $ftp->upload($f, $f);
  22. if($tmp){
  23. echo "upload $f -> success \n";
  24. }
  25. }
  26. //$ftp->download('ftp_upload.log','ftp_download.log');
  27. //
  28. //$ftp->upload('ftp_err.log','ftp_upload.log');
  29. //$ftp->download('ftp_upload.log','ftp_download.log');
  30. /*
  31. *
  32. *
  33. * $dir = "/test";
  34. if(@ftp_chdir($conn, $dir))
  35. 判断是否为文件夹
  36. * Enter description here ...
  37. * @author Administrator
  38. *
  39. */
  40. class FilerFile
  41. {
  42. var $time_path;
  43. private $fctimes = array();
  44. function Zip($dir)
  45. {
  46. $this->time_path = rtrim($dir,"/")."/.~~~time.php";
  47. //@unlink($this->time_path);
  48. $filelist = $this -> GetFileList($dir);
  49. file_put_contents($this->time_path,"fctimes,true).";");
  50. return $filelist;
  51. }
  52. function appendFilectime($file)
  53. {
  54. $time_file_path = $this->time_path;
  55. $ftime = @include($time_file_path);
  56. $ftime = $ftime ? $ftime : array();
  57. $time = filectime($file);
  58. if(!file_exists($time_file_path))file_put_contents($time_file_path," }
  59. function getFileByFilectime($file)
  60. {
  61. static $time_data ;
  62. $time_file_path = $this->time_path;
  63. if (!$time_data){
  64. $time_data= @include_once($time_file_path);
  65. }
  66. $time_data = $time_data ? $time_data : array();
  67. //var_dump($file,$time_data[$file] == filectime($file));
  68. //echo $file."\t".$time_data[$file]."\n";
  69. if ($time_data[$file] == filemtime($file)){
  70. return false;
  71. }else{
  72. return $file;
  73. }
  74. }
  75. function GetFileList($dir,$path="")
  76. {
  77. static $tmpp = "";
  78. if ($path=="" && !$tmpp){
  79. $tmpp = $dir;
  80. }
  81. $d = dir($dir);
  82. $files = array();
  83. if ($d)
  84. {
  85. $pathP=str_replace($tmpp,"",$dir);
  86. $pathP=str_replace(array("\\\\","/"),DIRECTORY_SEPARATOR,$pathP);
  87. $pathP=str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$pathP);
  88. while($f = $d->read())
  89. {
  90. if ($f == '.' || $f=='..' || $f{0}=='.' || $f=='Zend' || in_array($f, $_GET['exclude']))continue;
  91. $newdir = rtrim($dir,"/")."/".$f;
  92. if (is_dir($newdir)){
  93. $files = array_merge($files,$this->GetFileList($newdir,$newdir));
  94. }else{
  95. $abspath_file = (rtrim($dir,"/") ? rtrim($dir,"/")."/" : "").$f;
  96. $this->fctimes[$abspath_file] = filemtime($abspath_file);
  97. if (!$this->getFileByFilectime($abspath_file))continue;
  98. $file = (rtrim($pathP,"/") ? rtrim($pathP,"/")."/" : "").$f;
  99. $files[] = $file;
  100. }
  101. }
  102. }
  103. return $files;
  104. }
  105. }
  106. /**
  107. * 仿写CodeIgniter的FTP类
  108. * FTP基本操作:
  109. * 1) 登陆; connect
  110. * 2) 当前目录文件列表; filelist
  111. * 3) 目录改变; chgdir
  112. * 4) 重命名/移动; rename
  113. * 5) 创建文件夹; mkdir
  114. * 6) 删除; delete_dir/delete_file
  115. * 7) 上传; upload
  116. * 8) 下载 download
  117. *
  118. * @author quanshuidingdang
  119. */
  120. class Ftp {
  121. private $hostname = '';
  122. private $username = '';
  123. private $password = '';
  124. private $port = 21;
  125. private $passive = TRUE;
  126. private $debug = TRUE;
  127. private $conn_id = FALSE;
  128. /**
  129. * 构造函数
  130. *
  131. * @param array 配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);
  132. */
  133. public function __construct($config = array()) {
  134. if(count($config) > 0) {
  135. $this->_init($config);
  136. }
  137. }
  138. /**
  139. * FTP连接
  140. *
  141. * @access public
  142. * @param array 配置数组
  143. * @return boolean
  144. */
  145. public function connect($config = array()) {
  146. if(count($config) > 0) {
  147. $this->_init($config);
  148. }
  149. if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
  150. if($this->debug === TRUE) {
  151. $this->_error("ftp_unable_to_connect");
  152. }
  153. return FALSE;
  154. }
  155. if( ! $this->_login()) {
  156. if($this->debug === TRUE) {
  157. $this->_error("ftp_unable_to_login");
  158. }
  159. return FALSE;
  160. }
  161. if($this->passive === TRUE) {
  162. ftp_pasv($this->conn_id, TRUE);
  163. }
  164. return TRUE;
  165. }
  166. /**
  167. * 文件夹是否存在
  168. * @param unknown_type $path
  169. */
  170. public function is_dir_exists($path)
  171. {
  172. return $this->chgdir($path);
  173. }
  174. /**
  175. * 目录改变
  176. *
  177. * @access public
  178. * @param string 目录标识(ftp)
  179. * @param boolean
  180. * @return boolean
  181. */
  182. public function chgdir($path = '', $supress_debug = FALSE) {
  183. if($path == '' OR ! $this->_isconn()) {
  184. return FALSE;
  185. }
  186. $result = @ftp_chdir($this->conn_id, $path);
  187. if($result === FALSE) {
  188. if($this->debug === TRUE AND $supress_debug == FALSE) {
  189. $this->_error("ftp_unable_to_chgdir:dir[".$path."]");
  190. }
  191. return FALSE;
  192. }
  193. return TRUE;
  194. }
  195. /**
  196. * 目录生成
  197. *
  198. * @access public
  199. * @param string 目录标识(ftp)
  200. * @param int 文件权限列表
  201. * @return boolean
  202. */
  203. public function mkdir($path = '', $permissions = NULL) {
  204. if($path == '' OR ! $this->_isconn()) {
  205. return FALSE;
  206. }
  207. $result = @ftp_mkdir($this->conn_id, $path);
  208. if($result === FALSE) {
  209. if($this->debug === TRUE) {
  210. $this->_error("ftp_unable_to_mkdir:dir[".$path."]");
  211. }
  212. return FALSE;
  213. }
  214. if( ! is_null($permissions)) {
  215. $this->chmod($path,(int)$permissions);
  216. }
  217. return TRUE;
  218. }
  219. /**
  220. * 上传
  221. *
  222. * @access public
  223. * @param string 本地目录标识
  224. * @param string 远程目录标识(ftp)
  225. * @param string 上传模式 auto || ascii
  226. * @param int 上传后的文件权限列表
  227. * @return boolean
  228. */
  229. public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
  230. if( ! $this->_isconn()) {
  231. return FALSE;
  232. }
  233. if( ! file_exists($localpath)) {
  234. if($this->debug === TRUE) {
  235. $this->_error("ftp_no_source_file:".$localpath);
  236. }
  237. return FALSE;
  238. }
  239. if($mode == 'auto') {
  240. $ext = $this->_getext($localpath);
  241. $mode = $this->_settype($ext);
  242. }
  243. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  244. $result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);
  245. if($result === FALSE) {
  246. if($this->debug === TRUE) {
  247. $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
  248. }
  249. return FALSE;
  250. }
  251. if( ! is_null($permissions)) {
  252. $this->chmod($remotepath,(int)$permissions);
  253. }
  254. return TRUE;
  255. }
  256. /**
  257. * 下载
  258. *
  259. * @access public
  260. * @param string 远程目录标识(ftp)
  261. * @param string 本地目录标识
  262. * @param string 下载模式 auto || ascii
  263. * @return boolean
  264. */
  265. public function download($remotepath, $localpath, $mode = 'auto') {
  266. if( ! $this->_isconn()) {
  267. return FALSE;
  268. }
  269. if($mode == 'auto') {
  270. $ext = $this->_getext($remotepath);
  271. $mode = $this->_settype($ext);
  272. }
  273. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  274. $result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
  275. if($result === FALSE) {
  276. if($this->debug === TRUE) {
  277. $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");
  278. }
  279. return FALSE;
  280. }
  281. return TRUE;
  282. }
  283. /**
  284. * 重命名/移动
  285. *
  286. * @access public
  287. * @param string 远程目录标识(ftp)
  288. * @param string 新目录标识
  289. * @param boolean 判断是重命名(FALSE)还是移动(TRUE)
  290. * @return boolean
  291. */
  292. public function rename($oldname, $newname, $move = FALSE) {
  293. if( ! $this->_isconn()) {
  294. return FALSE;
  295. }
  296. $result = @ftp_rename($this->conn_id, $oldname, $newname);
  297. if($result === FALSE) {
  298. if($this->debug === TRUE) {
  299. $msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";
  300. $this->_error($msg);
  301. }
  302. return FALSE;
  303. }
  304. return TRUE;
  305. }
  306. /**
  307. * 删除文件
  308. *
  309. * @access public
  310. * @param string 文件标识(ftp)
  311. * @return boolean
  312. */
  313. public function delete_file($file) {
  314. if( ! $this->_isconn()) {
  315. return FALSE;
  316. }
  317. $result = @ftp_delete($this->conn_id, $file);
  318. if($result === FALSE) {
  319. if($this->debug === TRUE) {
  320. $this->_error("ftp_unable_to_delete_file:file[".$file."]");
  321. }
  322. return FALSE;
  323. }
  324. return TRUE;
  325. }
  326. /**
  327. * 删除文件夹
  328. *
  329. * @access public
  330. * @param string 目录标识(ftp)
  331. * @return boolean
  332. */
  333. public function delete_dir($path) {
  334. if( ! $this->_isconn()) {
  335. return FALSE;
  336. }
  337. //对目录宏的'/'字符添加反斜杠'\'
  338. $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
  339. //获取目录文件列表
  340. $filelist = $this->filelist($path);
  341. if($filelist !== FALSE AND count($filelist) > 0) {
  342. foreach($filelist as $item) {
  343. //如果我们无法删除,那么就可能是一个文件夹
  344. //所以我们递归调用delete_dir()
  345. if( ! @delete_file($item)) {
  346. $this->delete_dir($item);
  347. }
  348. }
  349. }
  350. //删除文件夹(空文件夹)
  351. $result = @ftp_rmdir($this->conn_id, $path);
  352. if($result === FALSE) {
  353. if($this->debug === TRUE) {
  354. $this->_error("ftp_unable_to_delete_dir:dir[".$path."]");
  355. }
  356. return FALSE;
  357. }
  358. return TRUE;
  359. }
  360. /**
  361. * 修改文件权限
  362. *
  363. * @access public
  364. * @param string 目录标识(ftp)
  365. * @return boolean
  366. */
  367. public function chmod($path, $perm) {
  368. if( ! $this->_isconn()) {
  369. return FALSE;
  370. }
  371. //只有在PHP5中才定义了修改权限的函数(ftp)
  372. if( ! function_exists('ftp_chmod')) {
  373. if($this->debug === TRUE) {
  374. $this->_error("ftp_unable_to_chmod(function)");
  375. }
  376. return FALSE;
  377. }
  378. $result = @ftp_chmod($this->conn_id, $perm, $path);
  379. if($result === FALSE) {
  380. if($this->debug === TRUE) {
  381. $this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");
  382. }
  383. return FALSE;
  384. }
  385. return TRUE;
  386. }
  387. /**
  388. * 获取目录文件列表
  389. *
  390. * @access public
  391. * @param string 目录标识(ftp)
  392. * @return array
  393. */
  394. public function filelist($path = '.') {
  395. if( ! $this->_isconn()) {
  396. return FALSE;
  397. }
  398. return ftp_nlist($this->conn_id, $path);
  399. }
  400. /**
  401. * 关闭FTP
  402. *
  403. * @access public
  404. * @return boolean
  405. */
  406. public function close() {
  407. if( ! $this->_isconn()) {
  408. return FALSE;
  409. }
  410. return @ftp_close($this->conn_id);
  411. }
  412. /**
  413. * FTP成员变量初始化
  414. *
  415. * @access private
  416. * @param array 配置数组
  417. * @return void
  418. */
  419. private function _init($config = array()) {
  420. foreach($config as $key => $val) {
  421. if(isset($this->$key)) {
  422. $this->$key = $val;
  423. }
  424. }
  425. //特殊字符过滤
  426. $this->hostname = preg_replace('|.+?://|','',$this->hostname);
  427. }
  428. /**
  429. * FTP登陆
  430. *
  431. * @access private
  432. * @return boolean
  433. */
  434. private function _login() {
  435. return @ftp_login($this->conn_id, $this->username, $this->password);
  436. }
  437. /**
  438. * 判断con_id
  439. *
  440. * @access private
  441. * @return boolean
  442. */
  443. private function _isconn() {
  444. if( ! is_resource($this->conn_id)) {
  445. if($this->debug === TRUE) {
  446. $this->_error("ftp_no_connection");
  447. }
  448. return FALSE;
  449. }
  450. return TRUE;
  451. }
  452. /**
  453. * 从文件名中获取后缀扩展
  454. *
  455. * @access private
  456. * @param string 目录标识
  457. * @return string
  458. */
  459. private function _getext($filename) {
  460. if(FALSE === strpos($filename, '.')) {
  461. return 'txt';
  462. }
  463. $extarr = explode('.', $filename);
  464. return end($extarr);
  465. }
  466. /**
  467. * 从后缀扩展定义FTP传输模式 ascii 或 binary
  468. *
  469. * @access private
  470. * @param string 后缀扩展
  471. * @return string
  472. */
  473. private function _settype($ext) {
  474. $text_type = array (
  475. 'txt',
  476. 'text',
  477. 'php',
  478. 'phps',
  479. 'php4',
  480. 'js',
  481. 'css',
  482. 'htm',
  483. 'html',
  484. 'phtml',
  485. 'shtml',
  486. 'log',
  487. 'xml'
  488. );
  489. return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
  490. }
  491. /**
  492. * 错误日志记录
  493. *
  494. * @access prvate
  495. * @return boolean
  496. */
  497. private function _error($msg) {
  498. 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);
  499. }
  500. }
  501. /*End of file ftp.php*/
  502. /*Location /Apache Group/htdocs/ftp.php*/
复制代码

上传文件, ftp, php


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

解释PHP中晚期静态结合的概念。 解释PHP中晚期静态结合的概念。 Mar 21, 2025 pm 01:33 PM

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章讨论了框架中的基本安全功能,以防止漏洞,包括输入验证,身份验证和常规更新。

描述扎实的原则及其如何应用于PHP的开发。 描述扎实的原则及其如何应用于PHP的开发。 Apr 03, 2025 am 12:04 AM

SOLID原则在PHP开发中的应用包括:1.单一职责原则(SRP):每个类只负责一个功能。2.开闭原则(OCP):通过扩展而非修改实现变化。3.里氏替换原则(LSP):子类可替换基类而不影响程序正确性。4.接口隔离原则(ISP):使用细粒度接口避免依赖不使用的方法。5.依赖倒置原则(DIP):高低层次模块都依赖于抽象,通过依赖注入实现。

自定义/扩展框架:如何添加自定义功能。 自定义/扩展框架:如何添加自定义功能。 Mar 28, 2025 pm 05:12 PM

本文讨论了将自定义功能添加到框架上,专注于理解体系结构,识别扩展点以及集成和调试的最佳实践。

如何用PHP的cURL库发送包含JSON数据的POST请求? 如何用PHP的cURL库发送包含JSON数据的POST请求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...

如何在系统重启后自动设置unixsocket的权限? 如何在系统重启后自动设置unixsocket的权限? Mar 31, 2025 pm 11:54 PM

如何在系统重启后自动设置unixsocket的权限每次系统重启后,我们都需要执行以下命令来修改unixsocket的权限:sudo...

See all articles