action_crawler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * 抓取远程图片
  4. * User: Jinqn
  5. * Date: 14-04-14
  6. * Time: 下午19:18
  7. */
  8. set_time_limit(0);
  9. include("Uploader.class.php");
  10. /* 上传配置 */
  11. $config = array(
  12. "pathFormat" => $CONFIG['catcherPathFormat'],
  13. "maxSize" => $CONFIG['catcherMaxSize'],
  14. "allowFiles" => $CONFIG['catcherAllowFiles'],
  15. "oriName" => "remote.png"
  16. );
  17. $fieldName = $CONFIG['catcherFieldName'];
  18. /* 抓取远程图片 */
  19. $list = array();
  20. if (isset($_POST[$fieldName])) {
  21. $source = $_POST[$fieldName];
  22. } else {
  23. $source = $_GET[$fieldName];
  24. }
  25. //上传设置(判断是否采用全路径)
  26. $attConfigInfo = \app\modules\admin\models\Config::find()->where("name='attachment'")->one();
  27. $attConfig = string2array($attConfigInfo->value);
  28. foreach ($source as $imgUrl) {
  29. $item = new Uploader($imgUrl, $config, "remote");
  30. $info = $item->getFileInfo();
  31. $fileUrl = $info['url'];
  32. $filePath = ltrim(str_replace(UPLOAD_URL,'',$fileUrl),'/');//文件相对路径
  33. $targetFile = str_replace('/',DIRECTORY_SEPARATOR,UPLOAD_PATH.$filePath);
  34. $filesize = filesize($targetFile);
  35. if(Yii::$app->params['oss']['OPEN_OSS']==1)//如果开启了存储
  36. {
  37. $oss = new \app\common\components\Oss();
  38. $initResult = $oss->init();
  39. ob_clean();
  40. if($initResult['error']==0)
  41. {
  42. $ossResult = $oss->Upload($targetFile,$filePath);
  43. if($ossResult['error']==0)
  44. {
  45. $fileUrl = $ossResult['data']['url'];
  46. $filePath = $attConfig['absolute_url']?$ossResult['data']['url']:$ossResult['data']['path'];
  47. @unlink($targetFile);
  48. $info['url'] = getFileUrl($filePath);
  49. }
  50. else
  51. {
  52. return json_encode([
  53. 'code' => 1,
  54. 'msg' => $ossResult['msg']
  55. ]);
  56. }
  57. }
  58. else
  59. {
  60. return json_encode([
  61. 'code' => 1,
  62. 'msg' => $initResult['msg']
  63. ]);
  64. }
  65. }
  66. else
  67. {
  68. $filePath = $attConfig['absolute_url']?$fileUrl:$filePath;
  69. }
  70. //写入附件表
  71. $attachment = new \app\modules\admin\models\Attachment();
  72. $attachment->file_name = basename($filePath);
  73. $attachment->file_path = $filePath;
  74. $attachment->file_size = $filesize;
  75. $attachment->file_ext = fileext(basename($filePath));
  76. $attachment->hash = md5($filePath);
  77. $attachment->upload_time = time();
  78. $attachment->upload_ip = ip();
  79. $attachment->status = 1;
  80. $attachment->list_order = 0;
  81. $attachment->folder = Yii::$app->request->post('folder','');
  82. $attachment->save();
  83. array_push($list, array(
  84. "state" => $info["state"],
  85. "url" => $info["url"],
  86. "size" => $info["size"],
  87. "title" => htmlspecialchars($info["title"]),
  88. "original" => htmlspecialchars($info["original"]),
  89. "source" => htmlspecialchars($imgUrl)
  90. ));
  91. }
  92. /* 返回抓取数据 */
  93. return json_encode(array(
  94. 'state'=> count($list) ? 'SUCCESS':'ERROR',
  95. 'list'=> $list
  96. ));