Token() public méthode

public Token ( $mac )
/**
 * Created by PhpStorm.
 * User: xingminglister
 * Date: 4/18/14
 * Time: 3:50 PM
 */
function upload_to_qiniu($filepath)
{
    require_once "qiniu/io.php";
    require_once "qiniu/rs.php";
    $bucket = "hitour";
    $qiniu_link = '';
    $names = pathinfo($filepath);
    $qiniu_key = md5($filepath) . (empty($names['extension']) ? '' : '.' . $names['extension']);
    $putPolicy = new Qiniu_RS_PutPolicy($bucket);
    $upToken = $putPolicy->Token(null);
    $putExtra = new Qiniu_PutExtra();
    $putExtra->Crc32 = 1;
    list($ret, $err) = Qiniu_PutFile($upToken, $qiniu_key, $filepath, $putExtra);
    //	echo "[" . (date('Ymd H:i:s', time())) . "] ====> Qiniu_PutFile result: \n";
    if ($err !== null) {
        if ($err->Code == 614) {
            $qiniu_link = 'http://hitour.qiniudn.com/' . $qiniu_key;
        }
        //		echo $filepath . "\n";
        //		var_dump($err);
    } else {
        //		var_dump($ret);
        if (!empty($ret['key'])) {
            $qiniu_link = 'http://hitour.qiniudn.com/' . $ret['key'];
        }
    }
    return $qiniu_link;
}
Exemple #2
0
 public function getUpToken()
 {
     require_once LIBPATH . 'Qiniu/rs.php';
     Qiniu_SetKeys($this->accessKey, $this->secretKey);
     $putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
     $this->upToken = $putPolicy->Token(null);
 }
Exemple #3
0
 public function upload_qiniu($file = "", $name = "")
 {
     $path = $_SERVER['DOCUMENT_ROOT'];
     require_once $path . "/app/libraries/qiniu/rs.php";
     require_once $path . "/app/libraries/qiniu/io.php";
     $bucket = "meilimei";
     $local_file = "upload/" . $name;
     $key = $local_file;
     $accessKey = 'tjtiZoMyJa9ggnNisrdocCCGvTlNLFvYxGMMQ6LF';
     $secretKey = '6zQXHont_jN-QFznn96H4oKA7IUqjibPeC-Yto62';
     Qiniu_SetKeys($accessKey, $secretKey);
     $putPolicy = new Qiniu_RS_PutPolicy($bucket);
     $upToken = $putPolicy->Token(null);
     if (move_uploaded_file($file, $local_file)) {
         $upload_file = $path . "/upload/" . $name;
         $putExtra = new Qiniu_PutExtra();
         $putExtra->Crc32 = 1;
         list($ret, $err) = Qiniu_PutFile($upToken, $key, $upload_file, $putExtra);
         if ($err !== null) {
             return false;
         } else {
             //var_dump($ret);
             return $ret;
         }
     } else {
         return false;
         // 			echo "move_uploaded错误!";
         // 			die;
     }
     return false;
 }
Exemple #4
0
 public function save($url, $config)
 {
     set_time_limit(0);
     if (empty($url)) {
         return '';
     }
     $ext = strrchr($url, ".");
     if ($ext != ".jpeg" && $ext != ".gif" && $ext != ".jpg" && $ext != ".png") {
         return "";
     }
     $filename = random(30) . $ext;
     if (!$this->check_remote_file_exists($url)) {
         return "";
     }
     $contents = @file_get_contents($url);
     $storename = $filename;
     $bu = $config['bucket'] . ":" . $storename;
     $accessKey = $config['access_key'];
     $secretKey = $config['secret_key'];
     Qiniu_SetKeys($accessKey, $secretKey);
     $putPolicy = new Qiniu_RS_PutPolicy($bu);
     $upToken = $putPolicy->Token(null);
     $putExtra = new Qiniu_PutExtra();
     $putExtra->Crc32 = 1;
     list($ret, $err) = Qiniu_Put($upToken, $storename, $contents, $putExtra);
     if (!empty($err)) {
         return "";
     }
     return 'http://' . trim($config['url']) . "/" . $ret['key'];
 }
Exemple #5
0
 public function getToken()
 {
     $client = new Qiniu_MacHttpClient(null);
     $putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
     $putPolicy->Expires = $this->deadline;
     $putPolicy->CallbackBody = 'key=$(key)&hash=$(etag)';
     $upToken = $putPolicy->Token(null);
     return $upToken;
 }
Exemple #6
0
 public function upload($key, $source)
 {
     Qiniu_SetKeys($this->accessKey, $this->secretKey);
     $putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
     $upToken = $putPolicy->Token(null);
     $putExtra = new Qiniu_PutExtra();
     $putExtra->Crc32 = 1;
     return Qiniu_PutFile($upToken, $key, $source, $putExtra);
 }
 public function buildUptoken($bucket, $key)
 {
     $accessKey = C('QINIU_ACCESS_KEY');
     $secretKey = C('QINIU_SECRET_KEY');
     Qiniu_setKeys($accessKey, $secretKey);
     $putPolicy = new \Qiniu_RS_PutPolicy($bucket . ':' . $key);
     $upToken = $putPolicy->Token(null);
     echo $upToken;
 }
Exemple #8
0
function Qiniu_RS_RputFile($self, $bucket, $key, $localFile, $putExtra)
{
    $putPolicy = new Qiniu_RS_PutPolicy("{$bucket}:{$key}");
    $upToken = $putPolicy->Token($self->Mac);
    if ($putExtra == null) {
        $putExtra = new Qiniu_Rio_PutExtra($bucket);
    } else {
        $putExtra->Bucket = $bucket;
    }
    return Qiniu_Rio_PutFile($upToken, $key, $localFile, $putExtra);
}
Exemple #9
0
 public function testStat()
 {
     $putPolicy = new Qiniu_RS_PutPolicy($this->bucket . ":" . $this->key);
     $upToken = $putPolicy->Token(null);
     list($ret, $err) = Qiniu_PutFile($upToken, $this->key, __FILE__, null);
     $this->assertNull($err);
     Qiniu_RS_Delete($this->client, $this->bucket, $this->notExistKey);
     list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $this->key);
     $this->assertArrayHasKey('hash', $ret);
     $this->assertNull($err);
     list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $this->notExistKey);
     $this->assertNull($ret);
     $this->assertFalse($err === null);
 }
 public function write($filename, $filePath)
 {
     $this->setBucketAndKey($filename);
     Qiniu_SetKeys($this->accessKey, $this->secretKey);
     $putPolicy = new \Qiniu_RS_PutPolicy($this->bucket);
     $upToken = $putPolicy->Token(null);
     $putExtra = new \Qiniu_PutExtra();
     $putExtra->Crc32 = 1;
     list($ret, $err) = Qiniu_PutFile($upToken, $this->key, $filePath, $putExtra);
     if ($err !== null) {
         throw new RuntimeException('Qiniu::write Error. ' . 'TargetFilename: ' . $filename . ', LocalFile: ' . $filePath . ', Reqid: ' . $err->Reqid . ', Err: ' . $err->Err, $err->Code);
     } else {
         return $ret['hash'];
     }
 }
Exemple #11
0
 /**
  * 设置路径
  * @param $key
  * @param null $returnUrl
  * @param null $returnBody
  * @return string
  */
 public function getSign($preKey = '', $returnUrl = null, $returnBody = null)
 {
     Qiniu_SetKeys(Yii::app()->params['QINIU_ACCESS_KEY'], Yii::app()->params['QINIU_SECRET_KEY']);
     $putPolicy = new Qiniu_RS_PutPolicy(Yii::app()->params['QINIU_BUCKET']);
     $putPolicy->Expires = 60 * 30;
     $putPolicy->SaveKey = $preKey . '$(etag)';
     $putPolicy->MimeLimit = 'image/*';
     if ($returnUrl !== null) {
         $putPolicy->CallbackUrl = $returnUrl;
     }
     if ($returnBody !== null) {
         $putPolicy->CallbackBody = urldecode(http_build_query($returnBody));
     }
     $upToken = $putPolicy->Token(null);
     return $upToken;
 }
Exemple #12
0
 public function testPut()
 {
     $key = 'testPut' . getTid();
     $err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
     $putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
     $upToken = $putPolicy->Token(null);
     list($ret, $err) = Qiniu_Put($upToken, $key, "hello world!", null);
     $this->assertNull($err);
     $this->assertArrayHasKey('hash', $ret);
     var_dump($ret);
     list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
     $this->assertNull($err);
     var_dump($ret);
     $err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
     $this->assertNull($err);
 }
Exemple #13
0
 /**
  * 上传文件
  * @param type $uploadFile 文本框上传文件名
  * @param type $qiniuFileName 存储在七牛里面的文件名
  */
 public function uploadFile($uploadFile, $qiniuFileName)
 {
     require_once 'qiniu/io.php';
     $file = $_FILES[$uploadFile]['tmp_name'];
     // 上传文件的位置
     $putPolicy = new Qiniu_RS_PutPolicy($this->_bucket);
     $upToken = $putPolicy->Token(null);
     $putExtra = new Qiniu_PutExtra();
     $putExtra->Crc32 = 1;
     list($ret, $err) = Qiniu_PutFile($upToken, $qiniuFileName, $file, $putExtra);
     if ($err !== null) {
         return array('succ' => false, 'data' => $err);
     } else {
         //            picThumb($accessKey, $secretKey, $key1, $width=100, $height=100);
         return array('succ' => true, 'data' => $ret);
     }
 }
Exemple #14
0
 public function testLargePut()
 {
     $key = 'testRioLargePut' . getTid();
     $err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
     $putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
     $upToken = $putPolicy->Token(null);
     $putExtra = new Qiniu_Rio_PutExtra($this->bucket);
     $reader = new MockReader();
     list($ret, $err) = Qiniu_Rio_Put($upToken, $key, $reader, QINIU_RIO_BLOCK_SIZE + 5, $putExtra);
     $this->assertNull($err);
     $this->assertEquals($ret['hash'], "lgQEOCZ8Ievliq8XOfZmWTndgOll");
     var_dump($ret);
     list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
     $this->assertNull($err);
     var_dump($ret);
     $err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
     $this->assertNull($err);
 }
Exemple #15
0
 function __construct($params)
 {
     /** 实例化*/
     $this->ci =& get_instance();
     require_once "qiniu/io.php";
     require_once "qiniu/rs.php";
     //
     $this->accessKey = $params['accesskey'];
     $this->secretKey = $params['secretkey'];
     $this->bucket = $params['bucket'];
     $this->file_domain = $params['file_domain'];
     $key = 'test.png';
     Qiniu_SetKeys($this->accessKey, $this->secretKey);
     $putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
     $this->upToken = $putPolicy->Token(null);
     $this->putExtra = new Qiniu_PutExtra();
     $this->putExtra->Crc32 = 1;
 }
 public function testPutWithPersistentOps()
 {
     $key = 'testPutWithPersistentOps' . getTid();
     $err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
     $putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
     $putPolicy->PersistentOps = 'avthumb/mp3';
     $putPolicy->PersistentNotifyUrl = 'http://someurl/abc';
     $upToken = $putPolicy->Token(null);
     list($ret, $err) = Qiniu_Put($upToken, $key, "hello world!", null);
     $this->assertNull($err);
     $this->assertArrayHasKey('hash', $ret);
     $this->assertArrayHasKey('persistentId', $ret);
     var_dump($ret);
     list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
     $this->assertNull($err);
     var_dump($ret);
     $err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
     $this->assertNull($err);
 }
Exemple #17
0
 public function doMobileUpload()
 {
     require_once "qiniu.config.php";
     global $_GPC;
     $serverId = $_GPC['serverId'];
     //获取音频链接
     $fileName = substr($serverId, -10) . '.amr';
     $url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $this->jssdk->getAccessToken() . '&media_id=' . $serverId;
     $body = $this->jssdk->HttpGet($url);
     // 上传至七牛
     Qiniu_SetKeys($qiniu['accessKey'], $qiniu['secretKey']);
     $putPolicy = new Qiniu_RS_PutPolicy($qiniu['bucket']);
     $upToken = $putPolicy->Token(null);
     list($ret, $err) = Qiniu_Put($upToken, $fileName, $body, null);
     if ($err !== null) {
         var_dump($err);
     } else {
         // var_dump($ret);
         echo 'http://' . $qiniu['domainName'] . '/' . $fileName;
     }
 }
Exemple #18
0
 /**
  * 文件上传
  * @param string $filePath 待上传文件的绝对路径
  * @return string 上传成功后的URL,失败时返回空
  */
 public function uploadFile($filePath)
 {
     $fileUrl = '';
     if (!file_exists($filePath)) {
         return $fileUrl;
     }
     $config = $this->config;
     $fileName = date('YmdHis_', $_SERVER['REQUEST_TIME']) . md5(PhalApi_Tool::createRandStr(8) . microtime(true));
     Qiniu_SetKeys($config['accessKey'], $config['secretKey']);
     $putPolicy = new Qiniu_RS_PutPolicy($config['space_bucket']);
     $upToken = $putPolicy->Token(null);
     $putExtra = new Qiniu_PutExtra();
     $putExtra->Crc32 = 1;
     list($ret, $err) = Qiniu_PutFile($upToken, $fileName, $filePath, $putExtra);
     if ($err !== null) {
         DI()->logger->debug('failed to upload file to qiniu', array('Err' => $err->Err, 'Reqid' => $err->Reqid, 'Details' => $err->Details, 'Code' => $err->Code));
     } else {
         $fileUrl = $config['space_host'] . '/' . $fileName;
         DI()->logger->debug('succeed to upload file to qiniu', $ret);
     }
     return $fileUrl;
 }
Exemple #19
0
 /**
  * 设置路径
  * @param string $preKey
  * @param string $suffix
  * @param null $returnUrl
  * @param null $returnBody
  * @param null $persistentOps
  * @param null $persistentUrl
  * @param null $pipeline
  * @return string
  */
 public function getSign($preKey = '', $suffix = '', $returnUrl = null, $returnBody = null, $persistentOps = null, $persistentUrl = null, $pipeline = null)
 {
     Qiniu_SetKeys(Yii::app()->params['QINIU']['ACCESS_KEY'], Yii::app()->params['QINIU']['SECRET_KEY']);
     $putPolicy = new Qiniu_RS_PutPolicy(Yii::app()->params['QINIU']['BUCKET']);
     $putPolicy->Expires = 60 * 30;
     $putPolicy->SaveKey = $preKey . '$(etag)' . $suffix;
     if ($returnUrl !== null) {
         $putPolicy->ReturnUrl = $returnUrl;
     }
     if ($returnBody !== null) {
         $putPolicy->ReturnBody = json_encode($returnBody);
     }
     if ($pipeline !== null) {
         $putPolicy->PersistentPipeline = $pipeline;
     }
     if ($persistentOps !== null && $persistentUrl !== null) {
         $putPolicy->PersistentOps = $persistentOps;
         $putPolicy->PersistentNotifyUrl = $persistentUrl;
     }
     $upToken = $putPolicy->Token(null);
     return $upToken;
 }
 /**
  * $filename 七牛可以手动填写路径
  */
 function sendToQiniu($path, $filename)
 {
     Qiniu_setKeys(self::$qiniuAccessKey, self::$qiniuSecretKey);
     $qiniu = new Qiniu_MacHttpClient(null);
     $putPolicy = new Qiniu_RS_PutPolicy(self::$bucket);
     $upToken = $putPolicy->Token(null);
     $putExtra = new Qiniu_PutExtra();
     $putExtra->Crc32 = 1;
     list($ret, $err) = Qiniu_PutFile($upToken, $filename, $path, $putExtra);
     if ($err) {
         return array('code' => -2, 'msg' => $err);
     } else {
         $file = $ret['key'];
         $domain = self::$bucket . '.qiniudn.com';
         $baseUrl = Qiniu_RS_MakeBaseUrl($domain, $file);
         $private = false;
         if ($private) {
             $getPolicy = new Qiniu_RS_GetPolicy();
             $baseUrl = $getPolicy->MakeRequest($baseUrl, null);
         }
         return array('code' => 0, 'msg' => $baseUrl);
     }
 }
Exemple #21
0
     $time = time();
     require_once '../qiniu/io.php';
     require_once '../qiniu/rs.php';
     $qiniu = (include '../qiniu/qiniu.php');
     require_once '../connect/PdoConnect.class.php';
     $db = (include '../connect/dbconfig.php');
     $accessKey = $qiniu['accessKey'];
     $secretKey = $qiniu['secretKey'];
     $bucket = $qiniu['bucket'];
     //2
     $pdo = new PDO_CON($db['dbtype'], $db['host'], $db['user'], $db['pasw'], $db['dbname']);
     $conn = $pdo->get_conId();
     $newname = time() . rand(10000, 99999) . '.jpg';
     Qiniu_SetKeys($accessKey, $secretKey);
     $putPolicy = new Qiniu_RS_PutPolicy($bucket);
     $upToken = $putPolicy->Token(null);
     list($ret, $err) = Qiniu_Put($upToken, $newname, file_get_contents($tmp_name), null);
     file_put_contents('./log', "ok", FILE_APPEND);
     $sql = "insert into ws_photo(picture,userid,time) values('{$newname}',{$uid},{$time})";
     $pdo->ExceSQL($sql, $conn);
 }
 /**
  * save to qiniu end
  */
 //		//移动文件
 //		$file_path = $save_path . $new_file_name;
 //		@chmod($file_path, 0644);//修改目录权限(Linux)
 //		if (move_uploaded_file($tmp_name, $file_path) === false) {//开始移动
 //			exit("返回错误: 上传文件失败。($file_name)");
 //		}
 //		  $file_url = $save_url . $new_file_name;
/**
 * 远程抓取
 * @param $uri
 * @param $config
 */
function getRemoteImage($uri, $config)
{
    //忽略抓取时间限制
    set_time_limit(0);
    //ue_separate_ue  ue用于传递数据分割符号
    $imgUrls = explode("ue_separate_ue", $uri);
    $tmpNames = array();
    foreach ($imgUrls as $imgUrl) {
        //http开头验证
        if (strpos($imgUrl, "http") !== 0) {
            array_push($tmpNames, "error");
            continue;
        }
        //获取请求头
        $heads = get_headers($imgUrl);
        //死链检测
        if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
            array_push($tmpNames, "error");
            continue;
        }
        //格式验证(扩展名验证和Content-Type验证)
        $fileType = strtolower(strrchr($imgUrl, '.'));
        if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) {
            array_push($tmpNames, "error");
            continue;
        }
        //打开输出缓冲区并获取远程图片
        ob_start();
        $context = stream_context_create(array('http' => array('follow_location' => false)));
        //请确保php.ini中的fopen wrappers已经激活
        readfile($imgUrl, false, $context);
        $img = ob_get_contents();
        ob_end_clean();
        //大小验证
        $uriSize = strlen($img);
        //得到图片大小
        $allowSize = 1024 * $config['maxSize'];
        if ($uriSize > $allowSize) {
            array_push($tmpNames, "error");
            continue;
        }
        //创建保存位置
        $savePath = $config['savePath'];
        if (!file_exists($savePath)) {
            mkdir("{$savePath}", 0777);
        }
        //写入文件
        //$tmpName = $savePath . rand( 1 , 10000 ) . time() . strrchr( $imgUrl , '.' );
        //            try {
        //                $fp2 = @fopen( $tmpName , "a" );
        //                fwrite( $fp2 , $img );
        //                fclose( $fp2 );
        //                array_push( $tmpNames ,  $tmpName );
        //            } catch ( Exception $e ) {
        //                array_push( $tmpNames , "error" );
        //            }
        /***
         * 保存图片文件到七牛
         * 1.引入七牛类文件
         * 2.引入七牛配置文件
         */
        require_once '../../qiniu/io.php';
        require_once '../../qiniu/rs.php';
        $qiniu = (include '../../qiniu/qiniu.php');
        $newname = time() . rand(10000, 99999) . '.jpg';
        $accessKey = $qiniu['accessKey'];
        $secretKey = $qiniu['secretKey'];
        $bucket = $qiniu['bucket'];
        Qiniu_SetKeys($accessKey, $secretKey);
        if (!is_object($putPolicy)) {
            $putPolicy = new Qiniu_RS_PutPolicy($bucket);
        }
        $upToken = $putPolicy->Token(null);
        list($ret, $err) = Qiniu_Put($upToken, $newname, $img, null);
        /**
         * 为blog中生成一条记录 返回blogid 插入到images中
         */
        if (!isset($_SESSION)) {
            session_start();
        }
        if (!empty($_SESSION['uid'])) {
            require_once '../../connect/PdoConnect.class.php';
            $db = (include '../../connect/dbconfig.php');
            $pdo = new PDO_CON($db['dbtype'], $db['host'], $db['user'], $db['pasw'], $db['dbname']);
            $conn = $pdo->get_conId();
            /**
             * 此处数据库中表名是写的死的   若有改变  请直接替换
             * 博文与图片是以一对多的关系   用session机制限制一对一关系
             * 一次发布文章动作成功后  注销session['blogid']
             */
            $uid = $_SESSION['uid'];
            if (empty($_SESSION['blogid'])) {
                $time = time();
                $sql = "insert into ws_blog(userid,time) values({$uid},{$time})";
                $res = $pdo->ExceSQL($sql, $conn);
                if ($res) {
                    $sql = "select id from ws_blog where userid = {$uid} and time = {$time} ";
                    $res = $pdo->ExceSQL($sql, $conn);
                    $id = $res[0]['id'];
                }
                $_SESSION['blogid'] = $id;
            } else {
                $id = $_SESSION['blogid'];
            }
            /**
             * 保存图片到image表
             */
            $sql = "insert into ws_image(blogid,image) value({$id},'{$newname}')";
            $res = $pdo->ExceSQL($sql, $conn);
        }
        /**
         * 插入到image表完成
         */
        //返回图片地址  编辑器中引用改地址
        //$url = "http://".$qiniu['bucket'].".".$qiniu['domain']."/".$newname."?".'imageMogr2/thumbnail/540x335!';
        //$url = "http://".$qiniu['bucket'].".".$qiniu['domain']."/".$newname;
        $url = $qiniu['resurl'] . $newname;
    }
    /**
     * 返回数据格式
     * {
     *   'url'   : '新地址一ue_separate_ue新地址二ue_separate_ue新地址三',
     *   'srcUrl': '原始地址一ue_separate_ue原始地址二ue_separate_ue原始地址三',
     *   'tip'   : '状态提示'
     * }
     */
    echo "{'url':'" . $url . "','tip':'远程图片抓取成功!','srcUrl':'" . $uri . "'}";
}
 public function actionUpmusic()
 {
     $songModel = new Song();
     if (isset($_POST['Song'])) {
         $songModel->attributes = $_POST['Song'];
         $songModel->userid = Yii::app()->session['uid'];
         if ($songModel->validate()) {
             if (!empty($_FILES['songname']['tmp_name'])) {
                 $file = $_FILES['songname'];
                 if (!MusicTypeCheck($file['name'], $file['size'])) {
                     Yii::app()->user->setFlash('upstatus', 'Sorry, 音乐文件大小或格式错误 :(');
                     $this->redirect(array("Upmusic"));
                     die;
                 }
                 $type = "." . GetFileExtension($file['name']);
                 Yii::import('application.vendors.*');
                 require_once 'Qiniu/rs.php';
                 require_once 'Qiniu/io.php';
                 $bucket = Yii::app()->params['bucket'];
                 $accessKey = Yii::app()->params['accessKey'];
                 $secretKey = Yii::app()->params['secretKey'];
                 $newname = time() . rand(10000, 99999) . $type;
                 //先保存记录
                 $songModel->song = $newname;
                 if ($songModel->save()) {
                     /**
                      * 
                      */
                 } else {
                     Yii::app()->user->setFlash('upstatus', 'Sorry,系统错误,上传音乐失败 :(');
                 }
                 Qiniu_SetKeys($accessKey, $secretKey);
                 $putPolicy = new Qiniu_RS_PutPolicy($bucket);
                 $upToken = $putPolicy->Token(null);
                 list($ret, $err) = Qiniu_Put($upToken, $newname, file_get_contents($file['tmp_name']), null);
                 if ($err === null) {
                     //成功
                     /***
                      * 
                      */
                     $this->redirect(array('admin/Imusic'));
                 } else {
                     //失败
                     Yii::app()->user->setFlash('upstatus', 'Sorry,系统错误,上传音乐失败 :(');
                 }
             }
         }
     }
     $data = array('songModel' => $songModel);
     $this->render("upmusic", $data);
 }
Exemple #24
0
 /**
  * Perform the file upload
  *
  * @return	bool
  */
 public function do_upload($field = 'userfile', $qiniu = FALSE)
 {
     // Is $_FILES[$field] set? If not, no reason to continue.
     if (!isset($_FILES[$field])) {
         $this->set_error('upload_no_file_selected');
         return FALSE;
     }
     // Is the upload path valid?
     if (!$this->validate_upload_path()) {
         // errors will already be set by validate_upload_path() so just return FALSE
         return FALSE;
     }
     // Was the file able to be uploaded? If not, determine the reason why.
     if (!is_uploaded_file($_FILES[$field]['tmp_name'])) {
         $error = !isset($_FILES[$field]['error']) ? 4 : $_FILES[$field]['error'];
         switch ($error) {
             case 1:
                 // UPLOAD_ERR_INI_SIZE
                 $this->set_error('upload_file_exceeds_limit');
                 break;
             case 2:
                 // UPLOAD_ERR_FORM_SIZE
                 $this->set_error('upload_file_exceeds_form_limit');
                 break;
             case 3:
                 // UPLOAD_ERR_PARTIAL
                 $this->set_error('upload_file_partial');
                 break;
             case 4:
                 // UPLOAD_ERR_NO_FILE
                 $this->set_error('upload_no_file_selected');
                 break;
             case 6:
                 // UPLOAD_ERR_NO_TMP_DIR
                 $this->set_error('upload_no_temp_directory');
                 break;
             case 7:
                 // UPLOAD_ERR_CANT_WRITE
                 $this->set_error('upload_unable_to_write_file');
                 break;
             case 8:
                 // UPLOAD_ERR_EXTENSION
                 $this->set_error('upload_stopped_by_extension');
                 break;
             default:
                 $this->set_error('upload_no_file_selected');
                 break;
         }
         return FALSE;
     }
     // Set the uploaded data as class variables
     $this->file_temp = $_FILES[$field]['tmp_name'];
     $this->file_size = $_FILES[$field]['size'];
     $this->_file_mime_type($_FILES[$field]);
     $this->file_type = preg_replace("/^(.+?);.*\$/", "\\1", $this->file_type);
     $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
     $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
     $this->file_ext = $this->get_extension($this->file_name);
     $this->client_name = $this->file_name;
     $this->fileMD5 = md5_file($this->file_temp);
     // Is the file type allowed to be uploaded?
     if (!$this->is_allowed_filetype()) {
         $this->set_error('upload_invalid_filetype');
         return FALSE;
     }
     // if we're overriding, let's now make sure the new name and type is allowed
     if ($this->_file_name_override != '') {
         $this->file_name = $this->_prep_filename($this->_file_name_override);
         // If no extension was provided in the file_name config item, use the uploaded one
         if (strpos($this->_file_name_override, '.') === FALSE) {
             $this->file_name .= $this->file_ext;
         } else {
             $this->file_ext = $this->get_extension($this->_file_name_override);
         }
         if (!$this->is_allowed_filetype(TRUE)) {
             $this->set_error('upload_invalid_filetype');
             return FALSE;
         }
     }
     // Convert the file size to kilobytes
     if ($this->file_size > 0) {
         $this->file_size = round($this->file_size / 1024, 2);
     }
     // Is the file size within the allowed maximum?
     if (!$this->is_allowed_filesize()) {
         $this->set_error('upload_invalid_filesize');
         return FALSE;
     }
     // Are the image dimensions within the allowed size?
     // Note: This can fail if the server has an open_basdir restriction.
     if (!$this->is_allowed_dimensions()) {
         $this->set_error('upload_invalid_dimensions');
         return FALSE;
     }
     // Sanitize the file name for security
     $this->file_name = $this->clean_file_name($this->file_name);
     // Truncate the file name if it's too long
     if ($this->max_filename > 0) {
         $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
     }
     // Remove white spaces in the name
     if ($this->remove_spaces == TRUE) {
         $this->file_name = preg_replace("/\\s+/", "_", $this->file_name);
     }
     /*
      * Validate the file name
      * This function appends an number onto the end of
      * the file if one with the same name already exists.
      * If it returns false there was a problem.
      */
     $this->orig_name = $this->file_name;
     if ($this->overwrite == FALSE) {
         $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
         if ($this->file_name === FALSE) {
             return FALSE;
         }
     }
     /*
      * Run the file through the XSS hacking filter
      * This helps prevent malicious code from being
      * embedded within a file.  Scripts can easily
      * be disguised as images or other file types.
      */
     if ($this->xss_clean) {
         if ($this->do_xss_clean() === FALSE) {
             $this->set_error('upload_unable_to_write_file');
             return FALSE;
         }
     }
     /*
      * Move the file to the final destination
      * To deal with different server configurations
      * we'll attempt to use copy() first.  If that fails
      * we'll use move_uploaded_file().  One of the two should
      * reliably work in most environments
      */
     //$fileMD5 = md5_file($this->file_temp);
     $profix = substr($this->fileMD5, 0, 2);
     $upload_old = $this->upload_path;
     $this->upload_path = $this->upload_path . $profix . "/";
     if (!file_exists($this->upload_path)) {
         mkdir($this->upload_path);
         chmod($this->upload_path, 0777);
     }
     if ($qiniu) {
         //加载七牛签名SDK
         require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR . 'third_party' . DIRECTORY_SEPARATOR . 'qiniu' . DIRECTORY_SEPARATOR . 'io.php';
         require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR . 'third_party' . DIRECTORY_SEPARATOR . 'qiniu' . DIRECTORY_SEPARATOR . 'rs.php';
         $key = JY_getUploadFileName("10086");
         $accessKey = 'pU2VbLy6emWXs5x8Kxy2ZIKFtCHID-ghDimXM9tl';
         $secretKey = '3N2Ngow0rsEFiJzK6EoHPljza3iP55GtTwsv2WyF';
         Qiniu_SetKeys($accessKey, $secretKey);
         $putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
         $upToken = $putPolicy->Token(null);
         $putExtra = new Qiniu_PutExtra();
         $putExtra->Crc32 = 1;
         list($ret, $err) = Qiniu_PutFile($upToken, $key, $this->file_temp, $putExtra);
         if ($err !== null) {
             $this->set_error('upload_destination_error');
         } else {
             $this->qiniuData = $ret;
         }
     } else {
         if (!@copy($this->file_temp, $this->upload_path . $this->file_name)) {
             if (!@move_uploaded_file($this->file_temp, $this->upload_path . $this->file_name)) {
                 $this->set_error('upload_destination_error');
                 return FALSE;
             }
         }
     }
     //还原upload目录
     $this->upload_path = $upload_old;
     /*
      * Set the finalized image dimensions
      * This sets the image width/height (assuming the
      * file was an image).  We use this information
      * in the "data" function.
      */
     $this->set_image_properties($this->upload_path . $this->file_name);
     return TRUE;
 }
Exemple #25
0
 /**
  * 上传文件到七牛
  * @param string $file 源文件  
  * @param string $upname 上传后的文件名  
  * @param string $bucket 七牛上传的位置 
  */
 public static function upFiletoQiniu($file, $upname, $bucket)
 {
     $path = Yii::getPathOfAlias('application');
     require_once $path . "/vendors/qiniu/rs.php";
     require_once $path . "/vendors/qiniu/io.php";
     $qiniu_config = Yii::app()->params['qiniuConfig'];
     $accessKey = $qiniu_config['ak'];
     $secretKey = $qiniu_config['sk'];
     Qiniu_SetKeys($accessKey, $secretKey);
     $putPolicy = new Qiniu_RS_PutPolicy($bucket);
     $upToken = $putPolicy->Token(null);
     $putExtra = new Qiniu_PutExtra();
     $putExtra->Crc32 = 1;
     list($ret, $err) = Qiniu_PutFile($upToken, $upname, $file, $putExtra);
     if ($err !== null) {
         return false;
     } else {
         return true;
     }
 }
Exemple #26
0
 public static function uploadFile($file, $content = null)
 {
     // 获取上传文件
     if (empty($file['name'])) {
         return false;
     }
     // 校验扩展名
     $part = explode('.', $file['name']);
     $ext = ($length = count($part)) > 1 ? strtolower($part[$length - 1]) : '';
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     // 获取插件配置
     $option = self::getConfig();
     $date = new Typecho_Date(Typecho_Widget::widget('Widget_Options')->gmtTime);
     // 保存位置
     $savepath = preg_replace(array('/\\{year\\}/', '/\\{month\\}/', '/\\{day\\}/'), array($date->year, $date->month, $date->day), $option->savepath);
     $savename = $savepath . sprintf('%u', crc32(uniqid())) . '.' . $ext;
     if (isset($content)) {
         $savename = $content['attachment']->path;
         self::deleteFile($savename);
     }
     // 上传文件
     $filename = $file['tmp_name'];
     if (!isset($filename)) {
         return false;
     }
     // 初始化 SDK
     self::initSDK($option->accesskey, $option->sercetkey);
     // 上传凭证
     $policy = new Qiniu_RS_PutPolicy($option->bucket);
     $token = $policy->Token(null);
     $extra = new Qiniu_PutExtra();
     $extra->Crc32 = 1;
     // 上传
     list($result, $error) = Qiniu_PutFile($token, $savename, $filename, $extra);
     if ($error == null) {
         return array('name' => $file['name'], 'path' => $savename, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($savename));
     } else {
         return false;
     }
 }
 public function actionToken()
 {
     $uid = Yii::app()->session['uid'];
     $file = $_POST['file'];
     Yii::import('application.vendors.*');
     require_once 'Qiniu/io.php';
     require_once 'Qiniu/rs.php';
     $bucket = Yii::app()->params['bucket'];
     $accessKey = Yii::app()->params['accessKey'];
     $secretKey = Yii::app()->params['secretKey'];
     $callBackUrl = Yii::app()->params['callBackUrl'];
     $callbackBody = Yii::app()->params['callbackBody'];
     Qiniu_SetKeys($accessKey, $secretKey);
     $putPolicy = new Qiniu_RS_PutPolicy($bucket);
     $putPolicy->SaveKey = $file;
     $putPolicy->CallbackUrl = $callBackUrl;
     $putPolicy->CallbackBody = "file={$file}&uid={$uid}";
     $upToken = $putPolicy->Token(null);
     echo $upToken;
 }
Exemple #28
0
 public static function setKeyContent($key, $content = null)
 {
     if (W2Qiniu::getKeyContent($key) != null) {
         if (!W2Qiniu::deleteFile($key)) {
             throw new Exception('无法删除文件', 1);
         }
     }
     Qiniu_SetKeys(W2Config::$Qiniu_accessKey, W2Config::$Qiniu_secretKey);
     $putPolicy = new Qiniu_RS_PutPolicy(W2Config::$Qiniu_bucket);
     $upToken = $putPolicy->Token(null);
     list($ret, $err) = Qiniu_Put($upToken, $key, $content, null);
     if ($err !== null) {
         if (defined('IS_SQL_PRINT') && IS_SQL_PRINT) {
             var_dump($err);
         }
         return false;
     } else {
         return true;
     }
 }
Exemple #29
0
function wpjam_qiniutek_put($key, $str)
{
    global $qiniutek_client;
    if (!$qiniutek_client) {
        $qiniutek_client = wpjam_get_qiniutek_client();
    }
    $wpjam_qiniutek = get_option('wpjam-qiniutek');
    $qiniutek_bucket = $wpjam_qiniutek['bucket'];
    $putPolicy = new Qiniu_RS_PutPolicy($qiniutek_bucket);
    $upToken = $putPolicy->Token(null);
    if (!function_exists('Qiniu_Put')) {
        require_once WP_CONTENT_DIR . "/plugins/wpjam-qiniu/sdk/io.php";
    }
    list($ret, $err) = Qiniu_Put($upToken, $key, $str, null);
    if ($err !== null) {
        $msg = ' 发生错误:<span style="color:red">' . $err->Err . '</span><br />';
    } else {
        $msg = ' 上传成功<br />';
    }
    return $msg;
}
Exemple #30
0
function uploadQiNiu($filePath)
{
    //这里写入七牛的空间名
    $bucket = "1111";
    //$bucket = "gavinwen-topic";
    $name = basename($filePath);
    //这里写入七牛的accessKey和secretKey
    $accessKey = 'sVv4Xk-11';
    $secretKey = '333-11222';
    Qiniu_SetKeys($accessKey, $secretKey);
    $putPolicy = new Qiniu_RS_PutPolicy($bucket);
    $upToken = $putPolicy->Token(null);
    $putExtra = new Qiniu_PutExtra();
    $putExtra->Crc32 = 1;
    list($ret, $err) = Qiniu_PutFile($upToken, $name, $filePath, $putExtra);
    //list($ret, $err) = Qiniu_PutFile($upToken, $key1, __file__, $putExtra);
    //echo "====> Qiniu_PutFile result: \n";
    if ($err !== null) {
        return false;
        //print_r($err);
    } else {
        $attachmentUrl = "http://{$bucket}.qiniudn.com/{$name}";
        return $attachmentUrl;
    }
}