CronAction.class.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. class CronAction extends Action {
  3. public function searchFile2Ftp( ){
  4. $dir = C('FTP_LOCAL_DIR');
  5. $targetDir = C('FTP_SERVER_DIR');
  6. $config = C('FTP_CONFIG');
  7. while (true) {
  8. $res = scanTargetFile($dir, 'md5');
  9. $files = explode(';', $res);
  10. array_pop($files);
  11. foreach ($files as $v) {
  12. $path_parts = pathinfo($v);
  13. $zip = $this->zip($path_parts['filename'], $path_parts['dirname']);
  14. if ($zip) {//压缩完成
  15. $tagetDat = $targetDir . '/' . $path_parts['filename'] . '.zip';
  16. echo $tagetDat . PHP_EOL;
  17. //删除本地dat文件
  18. @unlink($path_parts['dirname'] . '/' . $path_parts['filename'] . '.dat');
  19. $datRes = Zmcoding\FtpFile::getInstance($config)->up_file($zip, $tagetDat);
  20. if ($datRes) {
  21. $md5Res = Zmcoding\FtpFile::getInstance($config)->up_file($v, $targetDir . '/' . $path_parts['basename']); //md5文件
  22. if ($md5Res) {
  23. debug_log('upload_info', json_encode($targetDir . '/' . $path_parts['basename'], JSON_UNESCAPED_UNICODE));
  24. }
  25. debug_log('upload_info', json_encode($tagetDat, JSON_UNESCAPED_UNICODE));
  26. }
  27. }
  28. }
  29. // sleep(3);
  30. }
  31. }
  32. public function zip( $name, $path ){
  33. //压缩
  34. if (!file_exists($path)) {
  35. return false;
  36. }
  37. $current_dir = opendir($path);
  38. while (($file = readdir($current_dir)) !== false) {
  39. $sub_dir = $path . DIRECTORY_SEPARATOR . $file;
  40. if ($file == '.' || $file == '..') {
  41. continue;
  42. }
  43. if (is_file($sub_dir)) { //如果是.dat文件,进行赋值
  44. $pathinfo = pathinfo($file);
  45. if ($pathinfo['extension'] == 'dat') {
  46. $files[] = $file;
  47. }
  48. }
  49. }
  50. if (empty($files)) {
  51. return false;
  52. }
  53. // var_dump($files);exit;
  54. $zip_filename = $path . '/' . $name . '.zip';
  55. @unlink($zip_filename);
  56. if (!file_exists($zip_filename)) {
  57. touch($zip_filename);
  58. $zip = new \ZipArchive();
  59. $ret = $zip->open($zip_filename, \ZipArchive::OVERWRITE);
  60. if ($ret !== true) {
  61. return false;
  62. } else {
  63. if (!empty($files)) {
  64. foreach ($files as $file) {
  65. $zip->addFile($path . '/' . $file, $file);
  66. // @unlink($path . '/' . $name . '.dat');
  67. }
  68. }
  69. $zip->close();
  70. }
  71. }
  72. return $zip_filename;
  73. }
  74. }