Exemplo n.º 1
0
 public static function uploadAvatar($srcFile, $fileDir, $name, $suffix = 'png')
 {
     $settings = Yii::$app->params['settings'];
     if ($settings['upload_avatar'] === 'remote' && !empty($settings['upload_remote']) && !empty($settings['upload_remote_info'])) {
         if ($settings['upload_remote'] === 'upyun') {
             list($bucketName, $userName, $userPwd) = explode(',', $settings['upload_remote_info']);
             $upyun = new \app\lib\UpYun($bucketName, $userName, $userPwd);
             foreach (self::$avatarSizes as $key => $resize) {
                 list($width, $height) = explode('x', $resize);
                 $img = Image::thumbnail($srcFile, $width, $height)->get($suffix);
                 if (!$upyun->writeFile('/' . $fileDir . '/' . $name . '_' . $key . '.' . $suffix, $img, true)) {
                     return false;
                 }
             }
         } else {
             if ($settings['upload_remote'] === 'qiniu') {
                 list($bucketName, $accessKey, $secretKey) = explode(',', $settings['upload_remote_info']);
                 $upManager = new \Qiniu\Storage\UploadManager();
                 $auth = new \Qiniu\Auth($accessKey, $secretKey);
                 //			    $token = $auth->uploadToken($bucketName);
                 foreach (self::$avatarSizes as $key => $resize) {
                     list($width, $height) = explode('x', $resize);
                     $img = Image::thumbnail($srcFile, $width, $height)->get($suffix);
                     $token = $auth->uploadToken($bucketName . ':' . $fileDir . '/' . $name . '_' . $key . '.' . $suffix);
                     list($ret, $err) = $upManager->put($token, $fileDir . '/' . $name . '_' . $key . '.' . $suffix, $img);
                     if ($err !== null) {
                         Yii::error($err);
                         return false;
                     }
                 }
             }
         }
     } else {
         @mkdir($fileDir, 0755, true);
         foreach (self::$avatarSizes as $key => $resize) {
             list($width, $height) = explode('x', $resize);
             Image::thumbnail($srcFile, $width, $height)->save($fileDir . '/' . $name . '_' . $key . '.' . $suffix);
         }
     }
     return true;
 }
<?php

require_once "../../../bootstrap.php";
require_once "../../../utils.php";
use Qiniu\Storage;
//简单无key数据上传,设定mimeType和crc32校验
$data = "新年快乐, 七牛云存储!";
$auth = new \Qiniu\Auth($QINIU_ACCESS_KEY, $QINIU_SECRET_KEY);
$up_token = $auth->uploadToken($QINIU_PUBLIC_BUCKET);
$upload_manager = new \Qiniu\Storage\UploadManager();
$key = null;
//$key="happy_new_year_qiniu_cloud_storage.txt";
try {
    list($ret_data, $error) = $upload_manager->put($up_token, $key, $data, null, "text/plain", true);
    print_upload_result($ret_data, $error);
} catch (Exception $e) {
    print $e->getMessage();
}
Exemplo n.º 3
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
     $token = self::init($option->accesskey, $option->sercetkey)->uploadToken($option->bucket);
     // 上传
     $uploadMgr = new Qiniu\Storage\UploadManager();
     list($result, $error) = $uploadMgr->putFile($token, $savename, $filename);
     if ($error == null) {
         return array('name' => $file['name'], 'path' => $savename, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($savename));
     } else {
         var_dump($error);
         return false;
     }
 }
<?php

require_once "../../../bootstrap.php";
require_once "../../../utils.php";
use Qiniu\Storage;
//简单无key数据上传,设定扩展参数
$data = "最牛, 七牛云存储!";
$auth = new \Qiniu\Auth($QINIU_ACCESS_KEY, $QINIU_SECRET_KEY);
$up_token = $auth->uploadToken($QINIU_PUBLIC_BUCKET);
$upload_manager = new \Qiniu\Storage\UploadManager();
$key = null;
//$key="no1_qiniu_cloud_storage.txt";
$extra_params = array("x:device" => "pc", "x:date" => "2015/02/14", "x:name" => "jemy", "x:empty" => "", "hobby" => "programming");
try {
    list($ret_data, $error) = $upload_manager->put($up_token, $key, $data, $extra_params);
    print_upload_result($ret_data, $error);
} catch (Exception $e) {
    print $e->getMessage();
}
Exemplo n.º 5
0
function file_remote_upload($filename, $auto_delete_local = true)
{
    global $_W;
    if (empty($_W['setting']['remote']['type'])) {
        return false;
    }
    if ($_W['setting']['remote']['type'] == '1') {
        require_once IA_ROOT . '/framework/library/ftp/ftp.php';
        $ftp_config = array('hostname' => $_W['setting']['remote']['ftp']['host'], 'username' => $_W['setting']['remote']['ftp']['username'], 'password' => $_W['setting']['remote']['ftp']['password'], 'port' => $_W['setting']['remote']['ftp']['port'], 'ssl' => $_W['setting']['remote']['ftp']['ssl'], 'passive' => $_W['setting']['remote']['ftp']['pasv'], 'timeout' => $_W['setting']['remote']['ftp']['timeout'], 'rootdir' => $_W['setting']['remote']['ftp']['dir']);
        $ftp = new Ftp($ftp_config);
        if (true === $ftp->connect()) {
            $response = $ftp->upload(ATTACHMENT_ROOT . '/' . $filename, $filename);
            if ($auto_delete_local) {
                file_delete($filename);
            }
            if (!empty($response)) {
                return true;
            } else {
                return error(1, '远程附件上传失败,请检查配置并重新上传');
            }
        } else {
            return error(1, '远程附件上传失败,请检查配置并重新上传');
        }
    } elseif ($_W['setting']['remote']['type'] == '2') {
        require_once IA_ROOT . '/framework/library/alioss/sdk.class.php';
        $oss = new ALIOSS($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $_W['setting']['remote']['alioss']['ossurl']);
        $options = array(ALIOSS::OSS_FILE_UPLOAD => ATTACHMENT_ROOT . $filename, ALIOSS::OSS_PART_SIZE => 5242880);
        $response = $oss->create_mpu_object($_W['setting']['remote']['alioss']['bucket'], $filename, $options);
        if ($auto_delete_local) {
            file_delete($filename);
        }
        if ($response->status == 200) {
            return true;
        } else {
            return error(1, '远程附件上传失败,请检查配置并重新上传');
        }
    } elseif ($_W['setting']['remote']['type'] == '3') {
        require_once IA_ROOT . '/framework/library/qiniu/autoload.php';
        $auth = new Qiniu\Auth($_W['setting']['remote']['qiniu']['accesskey'], $_W['setting']['remote']['qiniu']['secretkey']);
        $uploadmgr = new Qiniu\Storage\UploadManager();
        $putpolicy = Qiniu\base64_urlSafeEncode(json_encode(array('scope' => $_W['setting']['remote']['qiniu']['bucket'] . ':' . $filename)));
        $uploadtoken = $auth->uploadToken($_W['setting']['remote']['qiniu']['bucket'], $filename, 3600, $putpolicy);
        list($ret, $err) = $uploadmgr->putFile($uploadtoken, $filename, ATTACHMENT_ROOT . '/' . $filename);
        if ($err !== null) {
            return error(1, '远程附件上传失败,请检查配置并重新上传');
        } else {
            return true;
        }
    }
}
<?php

require_once "../../../bootstrap.php";
require_once "../../../utils.php";
use Qiniu\Storage;
//简单无key文件上传,设定扩展参数
$auth = new \Qiniu\Auth($QINIU_ACCESS_KEY, $QINIU_SECRET_KEY);
//小文件,触发表单上传
$file_path = "/Users/jemy/Documents/qiniu.png";
$up_token = $auth->uploadToken($QINIU_PUBLIC_BUCKET);
$upload_manager = new \Qiniu\Storage\UploadManager();
$key = null;
$extra_params = array("x:device" => "pc", "x:date" => "2015/02/14", "x:name" => "jemy", "x:empty" => "", "hobby" => "programming");
try {
    list($ret_data, $error) = $upload_manager->putFile($up_token, $key, $file_path, $extra_params);
    print_upload_result($ret_data, $error);
} catch (Exception $e) {
    print $e->getMessage();
}
Exemplo n.º 7
0
<?php

require_once "../../../bootstrap.php";
require_once "../../../utils.php";
use Qiniu\Storage;
//简单无key数据上传
$data = "hello, 七牛云存储!";
$auth = new \Qiniu\Auth($QINIU_ACCESS_KEY, $QINIU_SECRET_KEY);
$up_token = $auth->uploadToken($QINIU_PUBLIC_BUCKET);
$upload_manager = new \Qiniu\Storage\UploadManager();
$key = null;
//$key="hello_qiniu_cloud_storage.txt";
try {
    list($ret_data, $error) = $upload_manager->put($up_token, $key, $data);
    print_upload_result($ret_data, $error);
} catch (Exception $e) {
    print $e->getMessage();
}
Exemplo n.º 8
0
        $ext = strtolower($tmp[$last]);
        //后缀名用小写
        if (!in_array($ext, array('jpg', 'png'))) {
            exit;
        }
        $new_file_path = $_FILES['file']['tmp_name'] . '.' . $ext;
        move_uploaded_file($_FILES["file"]["tmp_name"], $new_file_path);
        $tmp = FdHelperLib::crudApi('qiniu/auth', $written_language_tag, array('post' => array('nothing' => 1)));
        $token = $tmp['token'];
        // 要上传文件的本地路径
        $filePath = $new_file_path;
        // 上传到七牛后保存的文件名
        $newFilename = md5($new_file_path) . '.' . $ext;
        $key = 'shaixuan/' . $newFilename;
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new Qiniu\Storage\UploadManager();
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
    }
    $v['title'] = '';
    $v['img_filename'] = $newFilename;
    $v['img_server'] = ConfigParserLib::get('fd', 'img_server');
    $v['product_preview_img_width'] = ConfigParserLib::get('fd', 'product_preview_img_width');
    $v['product_preview_img_height'] = ConfigParserLib::get('fd', 'product_preview_img_height');
    $v['img_quality'] = ConfigParserLib::get('fd', 'img_quality');
    require FdHelperLib::getViewFilePath('add_img_process');
    exit;
}
//查看一个商品
if (0 < preg_match('/^\\/\\w+\\/[0-9]+$/', $uri)) {
    $page_tmp_dir = ConfigParserLib::get('tmp', 'tmp_dir') . md5($_SERVER['REQUEST_URI']) . '/';
    $ttl = ConfigParserLib::get('tmp', 'page_tmp_ttl');
<?php

require_once "../../../bootstrap.php";
require_once "../../../utils.php";
use Qiniu\Storage;
//简单无key文件上传,设定mimeType和crc32校验
$auth = new \Qiniu\Auth($QINIU_ACCESS_KEY, $QINIU_SECRET_KEY);
//小文件,触发表单上传
$file_path = "/Users/jemy/Documents/qiniu.png";
$up_token = $auth->uploadToken($QINIU_PUBLIC_BUCKET);
$upload_manager = new \Qiniu\Storage\UploadManager();
$key = null;
try {
    list($ret_data, $error) = $upload_manager->putFile($up_token, $key, $file_path, null, "image/png", true);
    print_upload_result($ret_data, $error);
} catch (Exception $e) {
    print $e->getMessage();
}
Exemplo n.º 10
0
 public function qiniu()
 {
     $accessKey = 'SFq5DgIU7IzesR-xs6JvwLOs7cZyDde3baLdu1Oj';
     $secretKey = 'gu--J_yaqKjdgNrfpmsUiE2nnEvza6TiIFyEGG2U';
     $auth = new \Qiniu\Auth($accessKey, $secretKey);
     $bucket = 'jetstar';
     $token = $auth->uploadToken($bucket);
     $filePath = UPLOADS . '/post/d.png';
     $key = 'her-php-logo.png';
     $uploadMgr = new \Qiniu\Storage\UploadManager();
     list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
     if ($err !== null) {
         dump($err);
     } else {
         dump($ret);
     }
 }