CronftpAction.class.php 3.0 KB

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