123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- /**
- * 抓取远程图片
- * User: Jinqn
- * Date: 14-04-14
- * Time: 下午19:18
- */
- set_time_limit(0);
- include("Uploader.class.php");
- /* 上传配置 */
- $config = array(
- "pathFormat" => $CONFIG['catcherPathFormat'],
- "maxSize" => $CONFIG['catcherMaxSize'],
- "allowFiles" => $CONFIG['catcherAllowFiles'],
- "oriName" => "remote.png"
- );
- $fieldName = $CONFIG['catcherFieldName'];
- /* 抓取远程图片 */
- $list = array();
- if (isset($_POST[$fieldName])) {
- $source = $_POST[$fieldName];
- } else {
- $source = $_GET[$fieldName];
- }
- //上传设置(判断是否采用全路径)
- $attConfigInfo = \app\modules\admin\models\Config::find()->where("name='attachment'")->one();
- $attConfig = string2array($attConfigInfo->value);
- foreach ($source as $imgUrl) {
- $item = new Uploader($imgUrl, $config, "remote");
- $info = $item->getFileInfo();
- $fileUrl = $info['url'];
- $filePath = ltrim(str_replace(UPLOAD_URL,'',$fileUrl),'/');//文件相对路径
- $targetFile = str_replace('/',DIRECTORY_SEPARATOR,UPLOAD_PATH.$filePath);
- $filesize = filesize($targetFile);
- if(Yii::$app->params['oss']['OPEN_OSS']==1)//如果开启了存储
- {
- $oss = new \app\common\components\Oss();
- $initResult = $oss->init();
- ob_clean();
- if($initResult['error']==0)
- {
- $ossResult = $oss->Upload($targetFile,$filePath);
- if($ossResult['error']==0)
- {
- $fileUrl = $ossResult['data']['url'];
- $filePath = $attConfig['absolute_url']?$ossResult['data']['url']:$ossResult['data']['path'];
- @unlink($targetFile);
- $info['url'] = getFileUrl($filePath);
- }
- else
- {
- return json_encode([
- 'code' => 1,
- 'msg' => $ossResult['msg']
- ]);
- }
- }
- else
- {
- return json_encode([
- 'code' => 1,
- 'msg' => $initResult['msg']
- ]);
- }
- }
- else
- {
- $filePath = $attConfig['absolute_url']?$fileUrl:$filePath;
- }
- //写入附件表
- $attachment = new \app\modules\admin\models\Attachment();
- $attachment->file_name = basename($filePath);
- $attachment->file_path = $filePath;
- $attachment->file_size = $filesize;
- $attachment->file_ext = fileext(basename($filePath));
- $attachment->hash = md5($filePath);
- $attachment->upload_time = time();
- $attachment->upload_ip = ip();
- $attachment->status = 1;
- $attachment->list_order = 0;
- $attachment->folder = Yii::$app->request->post('folder','');
- $attachment->save();
- array_push($list, array(
- "state" => $info["state"],
- "url" => $info["url"],
- "size" => $info["size"],
- "title" => htmlspecialchars($info["title"]),
- "original" => htmlspecialchars($info["original"]),
- "source" => htmlspecialchars($imgUrl)
- ));
- }
- /* 返回抓取数据 */
- return json_encode(array(
- 'state'=> count($list) ? 'SUCCESS':'ERROR',
- 'list'=> $list
- ));
|