CronAction.class.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $start = time();
  14. $zip = $this->zip($path_parts['filename'], $path_parts['dirname']);
  15. if ($zip) {//压缩完成
  16. echo '压缩用时:'.(time()-$start).PHP_EOL;
  17. $tagetDat = $targetDir . '/' . $path_parts['filename'] . '.zip';
  18. echo $tagetDat . PHP_EOL;
  19. //删除本地dat文件
  20. @unlink($path_parts['dirname'] . '/' . $path_parts['filename'] . '.dat');
  21. $datRes = Zmcoding\FtpFile::getInstance($config)->up_file($zip, $tagetDat);
  22. if ($datRes) {
  23. $md5Res = Zmcoding\FtpFile::getInstance($config)->up_file($v, $targetDir . '/' . $path_parts['basename']); //md5文件
  24. if ($md5Res) {
  25. debug_log('upload_info', json_encode($targetDir . '/' . $path_parts['basename'], JSON_UNESCAPED_UNICODE));
  26. }
  27. debug_log('upload_info', json_encode($tagetDat, JSON_UNESCAPED_UNICODE));
  28. }
  29. }
  30. }
  31. // sleep(3);
  32. }
  33. }
  34. public function zip( $name, $path ){
  35. //压缩
  36. if (!file_exists($path)) {
  37. return false;
  38. }
  39. $current_dir = opendir($path);
  40. $needzip = '';//需要压缩的文件
  41. while (($file = readdir($current_dir)) !== false) {
  42. $sub_dir = $path . DIRECTORY_SEPARATOR . $file;
  43. if ($file == '.' || $file == '..') {
  44. continue;
  45. }
  46. if (is_file($sub_dir)) { //如果是.dat文件,进行赋值
  47. $pathinfo = pathinfo($file);
  48. // var_dump($pathinfo);
  49. if ($pathinfo['extension'] == 'dat' && $pathinfo['filename'] == $name) {
  50. $needzip = $file;
  51. }
  52. }
  53. }
  54. if ($needzip == '') {
  55. return false;
  56. }
  57. // var_dump($files);exit;
  58. $zip_filename = $path . '/' . $name . '.zip';
  59. @unlink($zip_filename);
  60. if (!file_exists($zip_filename)) {
  61. touch($zip_filename);
  62. $zip = new \ZipArchive();
  63. $ret = $zip->open($zip_filename, \ZipArchive::OVERWRITE);
  64. if ($ret !== true) {
  65. return false;
  66. } else {
  67. // if (!empty($files)) {
  68. // foreach ($files as $file) {
  69. $zip->addFile($path . '/' . $needzip, $needzip);
  70. // @unlink($path . '/' . $name . '.dat');
  71. // }
  72. // }
  73. $zip->close();
  74. }
  75. }
  76. return $zip_filename;
  77. }
  78. }