CronAction.class.php 2.7 KB

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