12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- class CronAction 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);
- foreach ($files as $v) {
- $path_parts = pathinfo($v);
- $zip = $this->zip($path_parts['filename'], $path_parts['dirname']);
- if ($zip) {//压缩完成
- $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);
- 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);
- 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);
- if ($pathinfo['extension'] == 'dat') {
- $files[] = $file;
- }
- }
- }
- if (empty($files)) {
- 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 . '/' . $file, $file);
- // @unlink($path . '/' . $name . '.dat');
- }
- }
- $zip->close();
- }
- }
- return $zip_filename;
- }
-
- }
|