ホームページ バックエンド開発 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. 'パスワード' => '?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,"< ?php n return ".var_export ($this->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 までご連絡ください。

ホット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ヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

JSON Web Tokens(JWT)とPHP APIでのユースケースを説明してください。 JSON Web Tokens(JWT)とPHP APIでのユースケースを説明してください。 Apr 05, 2025 am 12:04 AM

JWTは、JSONに基づくオープン標準であり、主にアイデンティティ認証と情報交換のために、当事者間で情報を安全に送信するために使用されます。 1。JWTは、ヘッダー、ペイロード、署名の3つの部分で構成されています。 2。JWTの実用的な原則には、JWTの生成、JWTの検証、ペイロードの解析という3つのステップが含まれます。 3. PHPでの認証にJWTを使用する場合、JWTを生成および検証でき、ユーザーの役割と許可情報を高度な使用に含めることができます。 4.一般的なエラーには、署名検証障害、トークンの有効期限、およびペイロードが大きくなります。デバッグスキルには、デバッグツールの使用とロギングが含まれます。 5.パフォーマンスの最適化とベストプラクティスには、適切な署名アルゴリズムの使用、有効期間を合理的に設定することが含まれます。

PHPにおける後期静的結合の概念を説明します。 PHPにおける後期静的結合の概念を説明します。 Mar 21, 2025 pm 01:33 PM

記事では、PHP 5.3で導入されたPHPの後期静的結合(LSB)について説明し、より柔軟な継承を求める静的メソッドコールのランタイム解像度を可能にします。 LSBの実用的なアプリケーションと潜在的なパフォーマ

フレームワークセキュリティ機能:脆弱性から保護します。 フレームワークセキュリティ機能:脆弱性から保護します。 Mar 28, 2025 pm 05:11 PM

記事では、入力検証、認証、定期的な更新など、脆弱性から保護するためのフレームワークの重要なセキュリティ機能について説明します。

フレームワークのカスタマイズ/拡張:カスタム機能を追加する方法。 フレームワークのカスタマイズ/拡張:カスタム機能を追加する方法。 Mar 28, 2025 pm 05:12 PM

この記事では、フレームワークにカスタム機能を追加し、アーキテクチャの理解、拡張ポイントの識別、統合とデバッグのベストプラクティスに焦点を当てています。

PHPのCurlライブラリを使用してJSONデータを含むPOSTリクエストを送信する方法は? PHPのCurlライブラリを使用してJSONデータを含むPOSTリクエストを送信する方法は? Apr 01, 2025 pm 03:12 PM

PHP開発でPHPのCurlライブラリを使用してJSONデータを送信すると、外部APIと対話する必要があることがよくあります。一般的な方法の1つは、Curlライブラリを使用して投稿を送信することです。

確固たる原則と、それらがPHP開発にどのように適用されるかを説明してください。 確固たる原則と、それらがPHP開発にどのように適用されるかを説明してください。 Apr 03, 2025 am 12:04 AM

PHP開発における固体原理の適用には、次のものが含まれます。1。単一責任原則(SRP):各クラスは1つの機能のみを担当します。 2。オープンおよびクローズ原理(OCP):変更は、変更ではなく拡張によって達成されます。 3。Lischの代替原則(LSP):サブクラスは、プログラムの精度に影響を与えることなく、基本クラスを置き換えることができます。 4。インターフェイス分離原理(ISP):依存関係や未使用の方法を避けるために、細粒インターフェイスを使用します。 5。依存関係の反転原理(DIP):高レベルのモジュールと低レベルのモジュールは抽象化に依存し、依存関係噴射を通じて実装されます。

セッションのハイジャックはどのように機能し、どのようにPHPでそれを軽減できますか? セッションのハイジャックはどのように機能し、どのようにPHPでそれを軽減できますか? Apr 06, 2025 am 12:02 AM

セッションハイジャックは、次の手順で達成できます。1。セッションIDを取得します。2。セッションIDを使用します。3。セッションをアクティブに保ちます。 PHPでのセッションハイジャックを防ぐための方法には次のものが含まれます。1。セッション_regenerate_id()関数を使用して、セッションIDを再生します。2。データベースを介してストアセッションデータを3。

See all articles