FileHelper.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2018-12-05 12:23
  7. */
  8. namespace console\helpers;
  9. use Exception;
  10. class FileHelper extends \yii\helpers\FileHelper
  11. {
  12. public static function unzip($zipFile, $path)
  13. {
  14. if( !extension_loaded("zip") ){
  15. throw new Exception("zip extension not loaded");
  16. }
  17. if (!file_exists($zipFile)) {
  18. throw new Exception("File not exists " . $zipFile);
  19. }
  20. $resource = zip_open($zipFile);
  21. while ($dirResource = zip_read($resource)) {
  22. if (zip_entry_open($resource, $dirResource)) {
  23. $file_name = $path . zip_entry_name($dirResource);
  24. $file_path = substr($file_name, 0, strrpos($file_name, "/"));
  25. if (!is_dir($file_path)) {
  26. mkdir($file_path, 0777, true);
  27. }
  28. if (!is_dir($file_name)) {
  29. $fileSize = zip_entry_filesize($dirResource);
  30. $file_content = zip_entry_read($dirResource, $fileSize);
  31. file_put_contents($file_name, $file_content);
  32. }
  33. zip_entry_close($dirResource);
  34. }
  35. }
  36. zip_close($resource);
  37. }
  38. public static function request($url, $headers=[], $data=null)
  39. {
  40. if( !extension_loaded("curl") ){
  41. throw new Exception("curl extension not loaded");
  42. }
  43. $ch = curl_init();
  44. curl_setopt($ch,CURLOPT_URL, $url);
  45. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  46. //curl_setopt($ch,CURLOPT_SSLVERSION,3);
  47. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  48. if($data !== null){
  49. curl_setopt($ch, CURLOPT_POST, 1);
  50. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  51. }
  52. $data = curl_exec($ch);
  53. $error = curl_error($ch);
  54. curl_close($ch);
  55. return [$data, $error];
  56. }
  57. }