検索
ホームページバックエンド開発PHPチュートリアルFTPアップロードファイル用のphpスクリプト

大まかな原理 プロジェクト内の除外されていないすべてのファイルを反復処理し、文件修改时间晚于文件上一次修改时间 のファイルを取得します 次に、これらのファイルを 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. 'パスワード' => '?xxxxxxxxxxx', // FTP パスワード
  12. 'ポート' => 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->down​​load('ftp_upload.log','ftp_download.log');
  30. /*
  31. *
  32. *
  33. * $ dir = "/test";
  34. if(@ftp_chdir($conn, $dir))
  35. フォルダーかどうかを判断します
  36. * ここに説明を入力します...
  37. * @author 管理者
  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 appendFiletime($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 getFileByFiletime($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=='in_array($f, $_GET[' ']))続行;
  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->getFileByFiletime($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) ログイン; 接続
  110. * 2) 現在のディレクトリファイルリスト; filelist
  111. * 4) 名前変更/移動
  112. * 5 ) フォルダーの作成; mkdir
  113. * 6) 削除_dir/delete_file
  114. * 7) アップロード; アップロード
  115. * 8) ダウンロードダウンロード
  116. **/
  117. class Ftp {
  118. private $hostname = '';
  119. private $username = '';
  120. private $password = '';
  121. private $port = 21;
  122. private $passive = TRUE ;
  123. private $debug = TRUE;
  124. private $conn_id = FALSE;
  125. /**
  126. * コンストラクター
  127. *
  128. * @param array 設定配列: $config = array('hostname'=>'','username'=>'','password'=>'','port'= > ;''...);
  129. */
  130. public function __construct($config = array()) {
  131. if(count($config) > 0) {
  132. $this->_init($config);
  133. }
  134. }
  135. /**
  136. * FTP 接続
  137. *
  138. * @access public
  139. * @param 配列設定配列
  140. * @return boolean
  141. */
  142. public function connect($config = array()) {
  143. if(count($config) > 0) {
  144. $this->_init($config);
  145. }
  146. if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
  147. if($this->debug === TRUE) {
  148. $this->_error("ftp_unable_to_connect");
  149. }
  150. return FALSE;
  151. }
  152. if( ! $this->_login()) {
  153. if($this->debug === TRUE) {
  154. $this->_error("ftp_unable_to_login");
  155. }
  156. return FALSE;
  157. }
  158. if($this->passive === TRUE) {
  159. ftp_pasv($this->conn_id, TRUE);
  160. }
  161. return TRUE;
  162. }
  163. /**
  164. * フォルダーが存在するかどうか
  165. * @paramunknown_type $path
  166. */
  167. public function is_dir_exists($path)
  168. {
  169. return $this->gt;chgdir( $path);
  170. }
  171. /**
  172. * ディレクトリ変更
  173. *
  174. * @access public
  175. * @param 文字列ディレクトリ識別 (ftp)
  176. * @param boolean
  177. * @return boolean
  178. */
  179. public function chgdir($path = '', $supress_debug = FALSE) {
  180. if($path == '' OR ! $this->_isconn()) {
  181. return FALSE;
  182. }
  183. $result = @ftp_chdir($this->conn_id, $path);
  184. if($result === FALSE) {
  185. if($this ->debug === TRUE かつ $supress_debug == FALSE) {
  186. $this->_error("ftp_unable_to_chgdir:dir[".$path."]");
  187. }
  188. return FALSE;
  189. }
  190. return TRUE ;
  191. }
  192. /**
  193. * ディレクトリ生成
  194. *
  195. * @access public
  196. * @param 文字列ディレクトリ識別 (ftp)
  197. * @param int ファイル許可リスト
  198. * @return boolean
  199. */
  200. public function mkdir($path = '', $permissions = NULL) {
  201. if($path == '' OR ! $this->_isconn()) {
  202. return FALSE;
  203. }
  204. $result = @ftp_mkdir($this->conn_id, $path);
  205. if($result === FALSE) {
  206. if($this->debug === TRUE) {
  207. $this->_error("ftp_unable_to_mkdir:dir[".$path."]");
  208. }
  209. return FALSE;
  210. }
  211. if( ! is_null($permissions)) {
  212. $this->chmod( $path,(int)$permissions);
  213. }
  214. return TRUE;
  215. }
  216. /**
  217. * アップロード
  218. *
  219. * @access public
  220. * @param 文字列ローカルディレクトリ識別子
  221. * @param 文字列リモートディレクトリ識別子 (ftp)
  222. * @param 文字列アップロードモード auto || リストアップロード後の @param int ファイル権限
  223. * @return boolean
  224. */
  225. public function Upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
  226. if( ! $this->_isconn()) {
  227. return FALSE;
  228. }
  229. if( ! file_exists($localpath)) {
  230. if($this->debug === TRUE) {
  231. $this ->_error("ftp_no_source_file:".$localpath);
  232. }
  233. return FALSE;
  234. }
  235. if($mode == 'auto') {
  236. $ext = $this->_getext($localpath);
  237. $mode = $this->_settype($ext);
  238. }
  239. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  240. $result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);
  241. if($result === FALSE) {
  242. if($this->debug == = TRUE) {
  243. $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
  244. }
  245. return FALSE;
  246. }
  247. if( ! is_null ($permissions)) {
  248. $this->chmod($remotepath,(int)$permissions);
  249. }
  250. return TRUE;
  251. }
  252. /**
  253. * ダウンロード
  254. *
  255. * @access public
  256. * @param 文字列リモートディレクトリ識別 (ftp)
  257. * @param 文字列ローカルディレクトリ識別
  258. * @param 文字列ダウンロードモード auto || boolean
  259. */
  260. public function download($remotepath, $localpath, $mode = 'auto') {
  261. if( ! $this->_isconn()) {
  262. return FALSE;
  263. }
  264. if ($mode == 'auto') {
  265. $ext = $this->_getext($remotepath);
  266. $mode = $this->_settype($ext);
  267. }
  268. $mode = ($mode = = 'アスキー') ? FTP_ASCII : FTP_BINARY;
  269. $result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
  270. if($result === FALSE) {
  271. if($this->debug == = TRUE) {
  272. $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");
  273. }
  274. return FALSE;
  275. }
  276. return TRUE;
  277. }
  278. /**
  279. * 名前変更/移動
  280. *
  281. * @access public
  282. * @param string リモートディレクトリ識別 (ftp)
  283. * @param string 新しいディレクトリ識別
  284. * @param boolean 名前を変更する (FALSE) か移動する (TRUE) かを決定します
  285. * @return boolean
  286. */
  287. public function rename($oldname, $newname, $move = FALSE) {
  288. if( ! $this->_isconn()) {
  289. return FALSE;
  290. }
  291. $result = @ftp_rename($this->conn_id, $oldname, $newname);
  292. if($result === FALSE) {
  293. if($this->debug === TRUE) {
  294. $msg = ($移動 == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";
  295. $this->_error($msg);
  296. }
  297. return FALSE;
  298. }
  299. return TRUE;
  300. }
  301. /**
  302. * ファイルを削除
  303. *
  304. * @access public
  305. * @param 文字列ファイル識別子 (ftp)
  306. * @return boolean
  307. */
  308. public function delete_file($file) ) {
  309. if( ! $this->_isconn()) {
  310. return FALSE;
  311. }
  312. $result = @ftp_delete($this->conn_id, $file);
  313. if($result === FALSE) {
  314. if($this->debug === TRUE) {
  315. $this->_error("ftp_unable_to_delete_file:file[".$file."]");
  316. }
  317. return FALSE;
  318. }
  319. return TRUE ;
  320. }
  321. /**
  322. * フォルダーを削除
  323. *
  324. * @access public
  325. * @param 文字列ディレクトリ識別子 (ftp)
  326. * @return boolean
  327. */
  328. public function delete_dir($path) {
  329. if( ! $this->_isconn()) {
  330. return FALSE;
  331. }
  332. //对目录宏的'/ '字符追加反斜杠''
  333. $path = preg_replace("/(.+?)/*$/", "\1/", $path);
  334. //获取目录文件列表
  335. $filelist = $this ->filelist($path);
  336. if($filelist !== FALSE AND count($filelist) > 0) {
  337. foreach($filelist as $item) {
  338. //如果我们無法删除,那么就可能是一文件夹
  339. //所以我们递归调用delete_dir()
  340. if( ! @delete_file($item)) {
  341. $this->delete_dir($item);
  342. }
  343. }
  344. }
  345. //删除文件夹(空文件夹)
  346. $result = @ftp_rmdir($this->conn_id, $path);
  347. if($result === FALSE) {
  348. if($this->debug === TRUE) {
  349. $this->_error("ftp_unable_to_delete_dir:dir[".$path."]");
  350. }
  351. return FALSE;
  352. }
  353. return TRUE;
  354. }
  355. /**
  356. * ファイル権限を変更します
  357. *
  358. * @access public
  359. * @param 文字列ディレクトリ識別 (ftp)
  360. * @return boolean
  361. */
  362. public function chmod( $path, $perm) {
  363. if( ! $this->_isconn()) {
  364. return FALSE;
  365. }
  366. //ただ有るPHP5中決定才义了修改权制限的関数(ftp)
  367. if( ! function_exists('ftp_chmod')) {
  368. if($this ->debug === TRUE) {
  369. $this->_error("ftp_unable_to_chmod(function)");
  370. }
  371. return FALSE;
  372. }
  373. $result = @ftp_chmod($this->conn_id, $perm , $path);
  374. if($result === FALSE) {
  375. if($this->debug === TRUE) {
  376. $this->_error("ftp_unable_to_chmod:path[".$path." ]-chmod[".$perm."]");
  377. }
  378. return FALSE;
  379. }
  380. return TRUE;
  381. }
  382. /**
  383. * ディレクトリファイルリストを取得
  384. *
  385. * @access public
  386. * @param 文字列ディレクトリ識別 (ftp)
  387. * @return array
  388. */
  389. public function filelist($path = '.') {
  390. if( ! $this->_isconn()) {
  391. return FALSE;
  392. }
  393. return ftp_nlist($this->conn_id, $path);
  394. }
  395. /**
  396. * FTPを閉じる
  397. *
  398. * @access public
  399. * @return boolean
  400. */
  401. public function close () {
  402. if( ! $this->_isconn()) {
  403. return FALSE;
  404. }
  405. return @ftp_close($this->conn_id);
  406. }
  407. /**
  408. * FTPメンバー変数の初期化
  409. *
  410. * @access private
  411. * @param配列設定配列
  412. * @return void
  413. */
  414. プライベート関数 _init($config = array()) {
  415. foreach($config as $key => $val) {
  416. if(isset($this->$key) ) {
  417. $this->$key = $val;
  418. }
  419. }
  420. //特殊文字过滤
  421. $this->hostname = preg_replace('|.+?://|','',$this ->ホスト名);
  422. }
  423. /**
  424. * FTP ログイン
  425. *
  426. * @access private
  427. * @return boolean
  428. */
  429. プライベート関数 _login() {
  430. return @ftp_login($this->conn_id, $this->ユーザー名, $this->パスワード) ;
  431. }
  432. /**
  433. * 裁判官con_id
  434. *
  435. * @access private
  436. * @return boolean
  437. */
  438. プライベート関数 _isconn() {
  439. if( ! is_resource($this->gt;conn_id)) {
  440. if($this->debug === TRUE) {
  441. $this->_error("ftp_no_connection");
  442. }
  443. return FALSE;
  444. }
  445. return TRUE;
  446. }
  447. /**
  448. * ファイル名からサフィックス拡張子を取得します
  449. *
  450. * @access private
  451. * @param string ディレクトリ識別子
  452. * @return string
  453. */
  454. private function _getext($filename) {
  455. if(FALSE == = strpos($filename, '.')) {
  456. return 'txt';
  457. }
  458. $extarr =explode('.', $filename);
  459. return end($extarr);
  460. }
  461. /**
  462. * サフィックス拡張子から FTP 転送モードの ASCII またはバイナリを定義します
  463. *
  464. * @access private
  465. * @param 文字列サフィックス拡張子
  466. * @return string
  467. */
  468. プライベート関数_settype($ext) {
  469. $text_type = array (
  470. 'txt',
  471. 'text',
  472. 'php',
  473. 'phps',
  474. 'php4',
  475. 'js',
  476. ' css',
  477. 'htm',
  478. 'html',
  479. 'phtml',
  480. 'shtml',
  481. 'log',
  482. 'xml'
  483. );
  484. return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
  485. }
  486. /**
  487. * エラーログ
  488. *
  489. * @access prvate
  490. * @return boolean
  491. */
  492. private function _error($msg) {
  493. 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);
  494. }
  495. }
  496. /*ファイルの終わり ftp.php*/
  497. /*Location /Apache Group/htdocs/ftp.php*/
复制代

上传文件、ftp、php


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
アクション中のPHP:実際の例とアプリケーションアクション中のPHP:実際の例とアプリケーションApr 14, 2025 am 12:19 AM

PHPは、電子商取引、コンテンツ管理システム、API開発で広く使用されています。 1)eコマース:ショッピングカート機能と支払い処理に使用。 2)コンテンツ管理システム:動的コンテンツの生成とユーザー管理に使用されます。 3)API開発:RESTFUL API開発とAPIセキュリティに使用されます。パフォーマンスの最適化とベストプラクティスを通じて、PHPアプリケーションの効率と保守性が向上します。

PHP:インタラクティブなWebコンテンツを簡単に作成しますPHP:インタラクティブなWebコンテンツを簡単に作成しますApr 14, 2025 am 12:15 AM

PHPにより、インタラクティブなWebコンテンツを簡単に作成できます。 1)HTMLを埋め込んでコンテンツを動的に生成し、ユーザー入力またはデータベースデータに基づいてリアルタイムで表示します。 2)プロセスフォームの提出と動的出力を生成して、XSSを防ぐためにHTMLSPECIALCHARSを使用していることを確認します。 3)MySQLを使用してユーザー登録システムを作成し、Password_HashおよびPreprocessingステートメントを使用してセキュリティを強化します。これらの手法を習得すると、Web開発の効率が向上します。

PHPとPython:2つの一般的なプログラミング言語を比較しますPHPとPython:2つの一般的なプログラミング言語を比較しますApr 14, 2025 am 12:13 AM

PHPとPythonにはそれぞれ独自の利点があり、プロジェクトの要件に従って選択します。 1.PHPは、特にWebサイトの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンス、機械学習、人工知能に適しており、簡潔な構文を備えており、初心者に適しています。

PHPの永続的な関連性:それはまだ生きていますか?PHPの永続的な関連性:それはまだ生きていますか?Apr 14, 2025 am 12:12 AM

PHPは依然として動的であり、現代のプログラミングの分野で重要な位置を占めています。 1)PHPのシンプルさと強力なコミュニティサポートにより、Web開発で広く使用されています。 2)その柔軟性と安定性により、Webフォーム、データベース操作、ファイル処理の処理において顕著になります。 3)PHPは、初心者や経験豊富な開発者に適した、常に進化し、最適化しています。

PHPの現在のステータス:Web開発動向を見てくださいPHPの現在のステータス:Web開発動向を見てくださいApr 13, 2025 am 12:20 AM

PHPは、現代のWeb開発、特にコンテンツ管理とeコマースプラットフォームで依然として重要です。 1)PHPには、LaravelやSymfonyなどの豊富なエコシステムと強力なフレームワークサポートがあります。 2)パフォーマンスの最適化は、Opcacheとnginxを通じて達成できます。 3)PHP8.0は、パフォーマンスを改善するためにJITコンパイラを導入します。 4)クラウドネイティブアプリケーションは、DockerおよびKubernetesを介して展開され、柔軟性とスケーラビリティを向上させます。

PHP対その他の言語:比較PHP対その他の言語:比較Apr 13, 2025 am 12:19 AM

PHPは、特に迅速な開発や動的なコンテンツの処理に適していますが、データサイエンスとエンタープライズレベルのアプリケーションには良くありません。 Pythonと比較して、PHPはWeb開発においてより多くの利点がありますが、データサイエンスの分野ではPythonほど良くありません。 Javaと比較して、PHPはエンタープライズレベルのアプリケーションでより悪化しますが、Web開発により柔軟性があります。 JavaScriptと比較して、PHPはバックエンド開発により簡潔ですが、フロントエンド開発のJavaScriptほど良くありません。

PHP対Python:コア機能と機能PHP対Python:コア機能と機能Apr 13, 2025 am 12:16 AM

PHPとPythonにはそれぞれ独自の利点があり、さまざまなシナリオに適しています。 1.PHPはWeb開発に適しており、組み込みのWebサーバーとRich Functionライブラリを提供します。 2。Pythonは、簡潔な構文と強力な標準ライブラリを備えたデータサイエンスと機械学習に適しています。選択するときは、プロジェクトの要件に基づいて決定する必要があります。

PHP:Web開発の重要な言語PHP:Web開発の重要な言語Apr 13, 2025 am 12:08 AM

PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境