diff --git a/app/controller/ScriptController.php b/app/controller/ScriptController.php index 240ea0a..534a8e8 100644 --- a/app/controller/ScriptController.php +++ b/app/controller/ScriptController.php @@ -11,14 +11,16 @@ class ScriptController extends BaseController public function getArticle(){ //获取已就绪文章列表 - $list = Article::where('status', 2)->field("*")->select()->toArray(); + //$list = Article::where('status', 2)->field("*")->select()->toArray(); + $list = Article::where('status', 3)->field("*")->select()->toArray(); if(empty($list)){ - + echo 'none';die; } //创建待处理目录 $fileName = date('Y-m-d_H:i:s'); $pathSrc = getcwd() . '/uploads/' . $fileName; + $zipFile = $pathSrc . '.zip'; mkdir($pathSrc, 0777, true); foreach ($list as $value) { @@ -34,13 +36,13 @@ class ScriptController extends BaseController //url图片保存本地 if(empty($value['cover'])){ - copy(getcwd() . '/static/empty.png', $artSrc . '/b_封面图.jpg'); + copy(getcwd() . '/static/empty.png', $artSrc . '/b_cover.jpg'); }else{ $this->down_img($artSrc, $value['cover']); } //文章内容 - $wordname = $artSrc . "/a_文章.doc"; + $wordname = $artSrc . "/a_article.doc"; //html标签类文章转成word $html = $value['content']; @@ -49,17 +51,26 @@ class ScriptController extends BaseController $this->save($wordname); //设置信息 - file_put_contents($artSrc . '/c_信息.txt', $value['id'] . "\r\n" .$value['title']); + file_put_contents($artSrc . '/c_message.txt', $value['id'] . "\r\n" .$value['title']); //设置账号 - file_put_contents($artSrc . '/d_账号.txt', $this->getAccounts($value['account'])); + file_put_contents($artSrc . '/d_account.txt', $this->getAccounts($value['account'])); //更改状态为已下载 - Article::where('id', $value['id'])->update(['status' => 3]); + Article::where('id', $value['id'])->update([ + 'status' => 3, + 'download_time' => date('Y-m-d H:i:s') + ]); } + //打包 + $this->zip($pathSrc, $zipFile); + //下载 + $this->downloadZip($zipFile); + + die; } @@ -93,7 +104,7 @@ xmlns="http://www.w3.org/TR/REC-html40">'; readfile($img_url); $img=ob_get_contents(); ob_end_clean(); - $filename = '/b_封面图.jpg'; + $filename = '/b_cover.jpg'; $fp2=@fopen($path . $filename,'a'); fwrite($fp2,$img); fclose($fp2); @@ -111,5 +122,79 @@ xmlns="http://www.w3.org/TR/REC-html40">'; return implode("\r\n", $content); } - + + + //下载function + private function downloadZip($zipPath) + { + $zipPath = iconv("UTF-8", "GBK", $zipPath);//加这行中文文件夹也ok了 + header("Cache-Control: public"); + header("Content-Description: File Transfer"); + header('Content-disposition: attachment; filename=' . basename($zipPath)); //文件名 + header("Content-Type: application/zip"); //zip格式的 + header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件 + header('Content-Length: ' . filesize($zipPath)); //告诉浏览器,文件大小 + @readfile($zipPath);//ob_end_clean(); + @unlink(app()->getRootPath().'public/'.$zipPath);//删除压缩包 + } + + + /** + * zip压缩 + * @param string $zipfile 压缩文件目录 + * @param string $unzipdir 解压目录 + */ + public function zip($dirsrc, $zipfile){ + if(!is_dir($dirsrc)){ + return false; + } + + $aimDir = dirname($zipfile); + $this->createDir($aimDir); + + $zip = new \ZipArchive; + $res = $zip->open($zipfile, \ZipArchive::CREATE); + if($res !== true){ + return false; + } + + $this->_zip($dirsrc, $zip); + + $zip->close(); //关闭处理的zip文件 + return true; + } + + //递归将文件添加至压缩包 + private function _zip($path, $zip, $zip_path = ''){ + $path = rtrim($path, '/'); + $handler = opendir($path); //打开当前文件夹由$path指定。 + + while(($filename = readdir($handler)) !== false){ + if($filename == '.' || $filename == '..')continue; + + $nowPath = $path . '/' . $filename; + $thzip_path = $zip_path . $filename; + if(is_dir($nowPath)){ + // 如果读取的某个对象是文件夹,则递归 + $this->_zip($nowPath, $zip, $thzip_path . '/'); + }else{ + //将文件加入zip对象 + $zip->addFile($nowPath, $thzip_path); + } + } + + @closedir($path); + } + + /** + * 递归创建文件夹 + * @param string $path 路径 + */ + public function createDir($path){ + if (!file_exists($path)){ + $this->createDir(dirname($path)); + mkdir($path, 0777); + } + } + } \ No newline at end of file diff --git a/app/controller/index.php b/app/controller/index.php index ea039db..d9c7638 100644 --- a/app/controller/index.php +++ b/app/controller/index.php @@ -1,14 +1,247 @@ 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; +}