CronftpAction.class.php 2.7 KB

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