git 2 years ago
parent
commit
f0cefa7f93
1 changed files with 103 additions and 0 deletions
  1. 103 0
      Home/Lib/Action/CronftpAction.class.php

+ 103 - 0
Home/Lib/Action/CronftpAction.class.php

@@ -0,0 +1,103 @@
+<?php
+
+
+
+class CronftpAction extends Action {
+			
+
+
+ 
+	public  function searchFile2Ftp(  ){
+		$dir = C('FTP_LOCAL_DIR');
+		$targetDir = C('FTP_SERVER_DIR');
+		$config = C('FTP_CONFIG');
+		while (true) {
+			$res = scanTargetFile($dir, 'md5');
+			$files = explode(';', $res);
+			array_pop($files);
+			if(empty($files)){
+				sleep(1);
+			}
+			foreach ($files as $v) {
+				$path_parts = pathinfo($v);
+				//$start = time();
+				//判断是否需要压缩,之前压缩过但没上传成功,需要重新上传
+				$zip_file = $path_parts['dirname'].'/' . $path_parts['filename'] . '.zip';
+				if(file_exists($zip_file)){
+					$zip = $zip_file;
+				}else{
+					$zip = $this->zip($path_parts['filename'], $path_parts['dirname']);
+				}
+				if ($zip) {//压缩完成
+					if(filesize($zip) <= 0){
+						@unlink($zip);
+						continue;
+					}
+					//echo '压缩用时:'.(time()-$start).PHP_EOL;
+					$tagetDat = $targetDir . '/' . $path_parts['filename'] . '.zip';
+					//echo $tagetDat . PHP_EOL;
+					//删除本地dat文件
+					@unlink($path_parts['dirname'] . '/' . $path_parts['filename'] . '.dat');
+					$datRes = Zmcoding\FtpFile::getInstance($config)->up_file($zip, $tagetDat);
+					echo "datRes:".$datRes.PHP_EOL;
+					if ($datRes) {
+						$md5Res = Zmcoding\FtpFile::getInstance($config)->up_file($v,  $targetDir . '/' . $path_parts['basename']); //md5文件
+						if ($md5Res) {
+							debug_log('upload_info', json_encode($targetDir . '/' . $path_parts['basename'], JSON_UNESCAPED_UNICODE));
+						}
+						debug_log('upload_info', json_encode($tagetDat, JSON_UNESCAPED_UNICODE));
+					}
+				}
+			}
+			// sleep(3);
+		}
+	}
+	
+ 
+	public  function zip( $name, $path ){
+		//压缩
+		if (!file_exists($path)) {
+			return false;
+		}
+		$current_dir = opendir($path);
+		$needzip = '';//需要压缩的文件
+		while (($file = readdir($current_dir)) !== false) {
+			$sub_dir = $path . DIRECTORY_SEPARATOR . $file;
+			if ($file == '.' || $file == '..') {
+				continue;
+			}
+			if (is_file($sub_dir)) {    //如果是.dat文件,进行赋值
+				$pathinfo = pathinfo($file);
+				// var_dump($pathinfo);
+				if ($pathinfo['extension'] == 'dat' && $pathinfo['filename'] == $name) {
+					$needzip = $file;
+				}
+			}
+		}
+		if ($needzip == '') {
+			return false;
+		}
+		// var_dump($files);exit;
+		$zip_filename = $path . '/' . $name . '.zip';
+		@unlink($zip_filename);
+		if (!file_exists($zip_filename)) {
+			touch($zip_filename);
+			$zip = new \ZipArchive();
+			$ret = $zip->open($zip_filename, \ZipArchive::OVERWRITE);
+			if ($ret !== true) {
+				return false;
+			} else {
+				// if (!empty($files)) {
+				// foreach ($files as $file) {
+				$zip->addFile($path . '/' . $needzip, $needzip);
+				// @unlink($path . '/' . $name . '.dat');
+				// }
+				// }
+				$zip->close();
+			}
+		}
+		return $zip_filename;
+	}
+	
+
+}