cdnHelper.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * 上传文件到cdn源服务器
  4. *
  5. * 用法示范:
  6. * $sourceFile = "/data/wwwroot/nineke/admin/json/msg.json"; //本地目录
  7. * $res = oo::cdn()->uploadFile($sourceFile, 'json');
  8. * var_dump($res);
  9. */
  10. class cdnHelper
  11. {
  12. protected $key = ''; //与cdn上的php通信的秘钥
  13. protected $host = 'd147wns3pm1voh.cloudfront.net'; //源地址
  14. protected $url = '173.192.176.202/recvfile.php';
  15. protected $baseDir = '';
  16. public function __construct()
  17. {
  18. // date_default_timezone_set('Asia/Bangkok');
  19. date_default_timezone_set('Asia/Shanghai');
  20. $this->key = 'G8tvxS60UTGAoMgFaYvax1jzDkPPuW'.md5(date('YmdH').'^&_NINEKE_&^');
  21. $this->baseDir = oo::$config['cdnBaseDir'];
  22. }
  23. /**
  24. * 上传文件到cdn
  25. *
  26. * sourceFile: 本地文件全路径 /data/wwwroot/nineke/admin/json/A.json
  27. * dstDir: cdn目标文件夹(相对路径) json/
  28. * 泰语版本会上传到: /disk1/wwwroot/ipoker_cdnroot/static/nineke/nineke/json/A.json
  29. *
  30. * 返回值 ret=0:正常, 其余失败
  31. */
  32. public function uploadFile($sourceFile, $dstDir)
  33. {
  34. if (!$this->baseDir)
  35. {
  36. return -100;
  37. }
  38. $filename = basename($sourceFile);
  39. $path = "{$this->baseDir}{$dstDir}";
  40. $cmd = "curl -H'Host: {$this->host}' -F'f=@{$sourceFile}' -F'path={$path}' -F'key={$this->key}' -F'file_name={$filename}' {$this->url}";
  41. return shell_exec($cmd);
  42. }
  43. }