function __construct($config = array()) { parent::__construct($config); //初始化上传策略类 $this->put_policy->init(); }
public function __construct($config = array()) { parent::__construct($config); }
<?php include "connect.php"; include "token.php"; $user = checkToken(0, $result); if ($user == -1) { echo json_encode($result); return; } $qiniu = new Qiniu(); $result = array("token" => $qiniu->uploadToken()); echo json_encode($result); class Qiniu { private $accessKey = '7dh0-GZEY8xx5dpRMQBl19PGvE7zUixpJxUFhvVc'; private $secretKey = 'wzzcLKJMpgPa_UpQU4WCfW_wtti-58XSRxfaPtmv'; public function sign($data) { $hmac = hash_hmac('sha1', $data, $this->secretKey, true); return $this->accessKey . ':' . $this->base64_urlSafeEncode($hmac); } public function signWithData($data) { $data = $this->base64_urlSafeEncode($data); return $this->sign($data) . ':' . $data; } public function signRequest($urlString, $body, $contentType = null) { $url = parse_url($urlString); $data = ''; if (isset($url['path'])) {
<?php /** * Created by PhpStorm. * User: nathan * Date: 7/7/15 * Time: 7:29 PM */ header("Content-Type:text/json; charset=utf8"); include_once 'qiniu.php'; $token = Qiniu::getUploadToken(); echo '{"token":"' . $token . '"}';
<?php /** * 抓取远程图片 * User: rainemnt93 * Date: 2015-05-01 * Time: 下午16:18 */ set_time_limit(0); include "Qiniu_upload.php"; include "conf.php"; /* 上传配置 */ $fieldName = $CONFIG['catcherFieldName']; /* 生成上传实例对象并完成上传 */ $config = array('secrectKey' => $QINIU_SECRET_KEY, 'accessKey' => $QINIU_ACCESS_KEY, 'domain' => $HOST, 'bucket' => $BUCKET, 'timeout' => $TIMEOUT); $qiniu = new Qiniu($config); $list = array(); if (isset($_POST[$fieldName])) { $source = $_POST[$fieldName]; } else { $source = $_GET[$fieldName]; } $context = stream_context_create(array('http' => array('follow_location' => false))); foreach ($source as $imgUrl) { //命名规则 $key = time() . rand(0, 10) . '.png'; ob_start(); readfile($imgUrl, false, $context); $img = ob_get_contents(); ob_end_clean(); $upfile = array('name' => 'file', 'fileName' => $key, 'fileBody' => $img);
/** * 上传文件的主处理方法 * @return mixed */ private function upFile() { $file = $this->file = $_FILES[$this->fileField]; if (!$file) { $this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND"); return; } if ($this->file['error']) { $this->stateInfo = $this->getStateInfo($file['error']); return; } else { if (!file_exists($file['tmp_name'])) { $this->stateInfo = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND"); return; } else { if (!is_uploaded_file($file['tmp_name'])) { $this->stateInfo = $this->getStateInfo("ERROR_TMPFILE"); return; } } } $this->oriName = $file['name']; $this->fileSize = $file['size']; $this->fileType = $this->getFileExt(); $this->fullName = $this->getFullName(); $this->filePath = $this->getFilePath(); $this->fileName = $this->getFileName(); $dirname = dirname($this->filePath); //检查文件大小是否超出限制 if (!$this->checkSize()) { $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED"); return; } //检查是否不允许的文件格式 if (!$this->checkType()) { $this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED"); return; } if (DEFAULT_STORAGE) { $storage = new Qiniu(); $fileName = explode('/', $this->filePath); $fileName = array_pop($fileName); $destFileName = 'qimg_' . $fileName; $srcFileName = $file["tmp_name"]; //上传成功则返回URL,失败返回false $result = $storage->PutImgFile($destFileName, $srcFileName); if ($result == false) { $this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE"); } else { $this->fullName = $result; $this->filePath = $result; $this->stateInfo = $this->stateMap[0]; } } else { //创建目录失败 if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) { $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR"); return; } else { if (!is_writeable($dirname)) { $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE"); return; } } //移动文件 if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败 $this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE"); } else { //移动成功 $this->stateInfo = $this->stateMap[0]; } } }
/** * 通过url上传图片到七牛 * @param $profileImage * @param $userPhoto */ public function uploadFile($profileImageUrl, $userPhoto) { $imageUrl = (string) $profileImageUrl; //头像地址链接 $imageName = (string) $userPhoto; //图片文件拼接名称,如:user_photo/2015/01/30/1422587704_640x194.png //七牛的密钥、域名和空间 $ak = Yii::$app->params['ak']; $sk = Yii::$app->params['sk']; $domain = Yii::$app->params['qiniu_domain']; $bucket = Yii::$app->params['qiniu_bucket']; $qiNiu = new Qiniu($ak, $sk, $domain, $bucket); //登入到七牛 $ret = $qiNiu->uploadByUrl($imageUrl, $imageName); //$url = $qiNiu->getLink($imageName);//获取已上传到七牛的图片url return $ret; }