2023_01_10_104907_run_download_file.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Filesystem\Filesystem;
  4. class RunDownloadFile extends Migration
  5. {
  6. /**
  7. * Run the migrations.
  8. *
  9. * @return void
  10. */
  11. public function up()
  12. {
  13. $filesystem = app(Filesystem::class);
  14. //下载
  15. $url = 'https://downloads.yunzmall.com/company_backend/business_font.zip';
  16. $path = base_path('business/business_font.zip');
  17. if (file_exists(base_path('business/business_font'))) {
  18. return;
  19. }
  20. if (!file_exists($path)) {
  21. try {
  22. Utils::download($url, $path);
  23. \Log::debug('----business zip 下载ok----');
  24. } catch (\Exception $e) {
  25. \Log::debug('----business zip 下载失败----');
  26. return;
  27. }
  28. }
  29. //删除本地文件
  30. if (file_exists($path)) {
  31. $filesystem->deleteDirectory(base_path('business/business_font'));
  32. \Log::debug('----business dir delete----');
  33. }
  34. //解压
  35. if (file_exists($path)) {
  36. $zip = new \ZipArchive();
  37. $res = $zip->open($path);
  38. if ($res === true) {
  39. try {
  40. $zip->extractTo(base_path('business'));
  41. } catch (\Exception $e) {
  42. $zip->close();
  43. \Log::debug('----business zip 解压失败----');
  44. return false;
  45. }
  46. } else {
  47. $zip->close();
  48. \Log::debug('----business zip 解压下载失败----');
  49. return false;
  50. }
  51. $zip->close();
  52. \Log::debug('----business zip 解压ok----');
  53. }
  54. //删除压缩文件
  55. if (file_exists($path)) {
  56. @unlink($path);
  57. \Log::debug('----business zip 删除ok----');
  58. }
  59. }
  60. /**
  61. * Reverse the migrations.
  62. *
  63. * @return void
  64. */
  65. public function down()
  66. {
  67. //
  68. }
  69. }