 x-zj
					
					3 years ago
						x-zj
					
					3 years ago
					
				
				 2 changed files with 339 additions and 21 deletions
			
			
		| @ -1,14 +1,247 @@ | |||||
| <?php | <?php | ||||
|     try { |  | ||||
|         while (true) { |  | ||||
|             $url = 'http://article.com:70/sendArticle'; |  | ||||
|             $curl = curl_init(); |  | ||||
|             curl_setopt($curl, CURLOPT_URL, $url); |  | ||||
|             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |  | ||||
|             $res = json_decode(curl_exec($curl), true); |  | ||||
|             curl_close($curl); |  | ||||
|             sleep(10); |  | ||||
|         } |  | ||||
|     }catch(\Exception $e) { |  | ||||
|         echo $e->getMessage(); |  | ||||
|  | 
 | ||||
|  | $remoteUrl = 'https://article.kkxxkj.cn/api/getArticle'; | ||||
|  | $saveSrc = '/www/wwwroot/article.kkxxkj.cn/public/uploads'; | ||||
|  | 
 | ||||
|  | 
 | ||||
|  | try { | ||||
|  |     while (true) { | ||||
|  |         saveRemote($remoteUrl, $saveSrc); | ||||
|  |          | ||||
|  |         sleep(60); | ||||
|     } |     } | ||||
|  | }catch(\Exception $e) { | ||||
|  |     echo $e->getMessage(); | ||||
|  | } | ||||
|  | 
 | ||||
|  | 
 | ||||
|  | 
 | ||||
|  | //保存远程附件
 | ||||
|  | function saveRemote($remoteUrl, $saveSrc){ | ||||
|  | 	$fileinfo = getRemote($remoteUrl); | ||||
|  | 	$mimeType = $fileinfo['head']['content_type']; | ||||
|  | 	if($mimeType != 'application/zip')return false; | ||||
|  | 	//下载失败
 | ||||
|  | 	if(!isset($fileinfo['head']['http_code']) || $fileinfo['head']['http_code'] !== 200){ | ||||
|  | 		return false; | ||||
|  | 	} | ||||
|  | 	 | ||||
|  | 	$fileName = $saveSrc . '/tmp/' . uniqid() . '.zip'; | ||||
|  | 	$res = setContent($fileName, $fileinfo['body']); //保存
 | ||||
|  | 	if(!$res)return false; | ||||
|  | 	 | ||||
|  | 	//解压
 | ||||
|  | 	unzip($fileName, $saveSrc . '/ready'); | ||||
|  | } | ||||
|  | 	 | ||||
|  | 	 | ||||
|  | //获取远程文件信息
 | ||||
|  | function getRemote($url){ | ||||
|  | 	$ch = curl_init($url); | ||||
|  | 	curl_setopt($ch, CURLOPT_HEADER, 0); | ||||
|  | 	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); | ||||
|  | 	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||||
|  | 	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | ||||
|  | 	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | ||||
|  | 	$package = curl_exec($ch); | ||||
|  | 	$httpinfo = curl_getinfo($ch); | ||||
|  | 	curl_close($ch); | ||||
|  | 
 | ||||
|  | 	return [ | ||||
|  | 		'body' => $package, | ||||
|  | 		'head' => $httpinfo | ||||
|  | 	]; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /** | ||||
|  | * 存储文件内容 | ||||
|  | * @param string $filesrc 文件路径 | ||||
|  | * @param string $content 内容 | ||||
|  | */ | ||||
|  | function setContent($filesrc, $content){ | ||||
|  | 	createDir(dirname($filesrc)); | ||||
|  | 
 | ||||
|  | 	return file_put_contents($filesrc, $content) !== false ? true : false; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /** | ||||
|  | * 递归创建文件夹 | ||||
|  | * @param string $path 路径 | ||||
|  | */ | ||||
|  | function createDir($path){  | ||||
|  | 	if (!file_exists($path)){  | ||||
|  | 		createDir(dirname($path)); | ||||
|  | 		mkdir($path, 0777);  | ||||
|  | 	}  | ||||
|  | }  | ||||
|  | 
 | ||||
|  | 
 | ||||
|  | /** | ||||
|  |  * zip解压 | ||||
|  |  * @param string $zipfile 压缩文件目录 | ||||
|  |  * @param string $unzipdir 解压目录 | ||||
|  |  */ | ||||
|  | function unzip($zipfile, $unzipdir){ | ||||
|  | 	//判断压缩文件是否存在
 | ||||
|  | 	if(!file_exists($zipfile)){ | ||||
|  | 		return false; | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	//判断解压目录是否存在
 | ||||
|  | 	if(is_dir($unzipdir)){ | ||||
|  | 		deldir($unzipdir); | ||||
|  | 		createDir($unzipdir); | ||||
|  | 	}else{ | ||||
|  | 		createDir($unzipdir); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	$zip = new \ZipArchive; | ||||
|  | 	$res = $zip->open($zipfile); | ||||
|  | 	if($res !== true){ | ||||
|  | 		return false; | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	$docnum = $zip->numFiles; | ||||
|  | 	$ret = true; | ||||
|  | 	for($i = 0; $i < $docnum; $i++) { | ||||
|  | 		$statInfo = $zip->statIndex($i); | ||||
|  | 		 | ||||
|  | 		if($statInfo['crc'] == 0 && $statInfo['comp_size'] == 0) { | ||||
|  | 			//目录
 | ||||
|  | 			createDir(iconv('UTF-8', 'GB2312', $unzipdir.'/'.$statInfo['name']), 0777, true); | ||||
|  | 		} else { | ||||
|  | 			//文件
 | ||||
|  | 			//拷贝文件,特别的改动,iconv的位置决定copy能不能work
 | ||||
|  | 			$fileName = iconv('UTF-8', 'GB2312', $unzipdir.'/'.$statInfo['name']); | ||||
|  | 			$aimDir = dirname($fileName); | ||||
|  | 	        createDir($aimDir); | ||||
|  | 			 | ||||
|  | 			if(copy('zip://'.$zipfile.'#'.$statInfo['name'], $fileName) == false){ | ||||
|  | 				$ret = false; | ||||
|  | 				break; | ||||
|  | 			} | ||||
|  | 		} | ||||
|  | 	} | ||||
|  | 	$zip->close(); | ||||
|  | 
 | ||||
|  | 	if(!$ret){ | ||||
|  | 		return false; | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	//解决压缩方式问题
 | ||||
|  | 	$fileArrTemp = scandir($unzipdir); | ||||
|  | 	$fileArr = []; | ||||
|  | 	for($i = 0; $i < count($fileArrTemp); $i++){ | ||||
|  | 		if($fileArrTemp[$i] != '.' && $fileArrTemp[$i] != '..'){ | ||||
|  | 			$fileArr[] = $fileArrTemp[$i]; | ||||
|  | 		} | ||||
|  | 	} | ||||
|  | 	if(count($fileArr) == 1){ | ||||
|  | 		moveDir($unzipdir . $fileArr[0], $unzipdir, true); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	return true; | ||||
|  | } | ||||
|  | 
 | ||||
|  | //删除文件夹
 | ||||
|  | function deldir($dir) {  | ||||
|  | 	//先删除目录下的文件:
 | ||||
|  | 	if(!is_dir($dir)){ | ||||
|  | 		return true; | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	$dh = opendir($dir);  | ||||
|  | 	while($file = readdir($dh)) { | ||||
|  | 		if($file != "." && $file != "..") { | ||||
|  | 			$fullpath = $dir."/".$file;  | ||||
|  | 			if(!is_dir($fullpath)) {  | ||||
|  | 				unlink($fullpath);  | ||||
|  | 			} else {  | ||||
|  | 				deldir($fullpath);  | ||||
|  | 			}  | ||||
|  | 		}  | ||||
|  | 	}  | ||||
|  | 	 | ||||
|  | 	closedir($dh);  | ||||
|  | 	//删除当前文件夹: 
 | ||||
|  | 	if(rmdir($dir)) {  | ||||
|  | 		return true;  | ||||
|  | 	} else {  | ||||
|  | 		return false;  | ||||
|  | 	}  | ||||
|  | } | ||||
|  | 
 | ||||
|  | /** | ||||
|  | * 移动文件夹 | ||||
|  | * | ||||
|  | * @param string $oldDir | ||||
|  | * @param string $aimDir | ||||
|  | * @param boolean $overWrite 该参数控制是否覆盖原文件 | ||||
|  | * @return boolean | ||||
|  | */ | ||||
|  | function moveDir($oldDir, $aimDir, $overWrite = false, $chmod = false) { | ||||
|  | 	$aimDir = str_replace('', '/', $aimDir); | ||||
|  | 	$aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/'; | ||||
|  | 	$oldDir = str_replace('', '/', $oldDir); | ||||
|  | 	$oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/'; | ||||
|  | 
 | ||||
|  | 	if (!is_dir($oldDir)) { | ||||
|  | 		return false; | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	if (!file_exists($aimDir)) { | ||||
|  | 		createDir($aimDir); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@$dirHandle = opendir($oldDir); | ||||
|  | 
 | ||||
|  | 	if (!$dirHandle) { | ||||
|  | 		return false; | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	while (false !== ($file = readdir($dirHandle))) { | ||||
|  | 		if ($file == '.' || $file == '..') { | ||||
|  | 			continue; | ||||
|  | 		} | ||||
|  | 
 | ||||
|  | 		if (!is_dir($oldDir . $file)) { | ||||
|  | 			moveFile($oldDir . $file, $aimDir . $file, $overWrite, $chmod); | ||||
|  | 		} else { | ||||
|  | 			moveDir($oldDir . $file, $aimDir . $file, $overWrite); | ||||
|  | 		} | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	closedir($dirHandle); | ||||
|  | 
 | ||||
|  | 	return rmdir($oldDir); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /** | ||||
|  | * 移动文件 | ||||
|  | * | ||||
|  | * @param string $fileUrl | ||||
|  | * @param string $aimUrl | ||||
|  | * @param boolean $overWrite 该参数控制是否覆盖原文件 | ||||
|  | * @return boolean | ||||
|  | */ | ||||
|  | function moveFile($fileUrl, $aimUrl, $overWrite = false, $chmod = false) { | ||||
|  | 	if (!file_exists($fileUrl)) { | ||||
|  | 		return false; | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	if (file_exists($aimUrl) && $overWrite = false) { | ||||
|  | 		return false; | ||||
|  | 	} elseif (file_exists($aimUrl) && $overWrite = true) { | ||||
|  | 		@unlink($aimUrl); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	$aimDir = dirname($aimUrl); | ||||
|  | 	createDir($aimDir); | ||||
|  | 	rename($fileUrl, $aimUrl); | ||||
|  | 
 | ||||
|  | 	if($chmod){ | ||||
|  | 		@chmod($aimUrl, 0777); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	return true; | ||||
|  | } | ||||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue