CronAction.class.php 2.6 KB

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