PHP上傳文件及圖片到七牛的方法
更新時(shí)間:2018年07月25日 10:13:23 作者:PMPSSPMP
這篇文章主要介紹了PHP上傳文件及圖片到七牛的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
上傳文件到七牛最簡單的方式就是使用七牛官方最新的SDK
安裝PHP SDK
composer require qiniu/php-sdk
上傳文件到七牛
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
$cfg = [
'access' => 'YOUR_ACCESS_KEY',
'secret' => 'YOUR_SECRET_KEY',
'bucket' => 'YOUR_BUCKET',
'domain' => 'https://images.your_domain.com'
];
$auth = new Auth($cfg['access'], $cfg['secret']);
// 創(chuàng)建一個(gè)過期時(shí)間為1小時(shí)的臨時(shí)上傳令牌
$token = $auth->uploadToken($cfg['bucket'], null, 3600);
$filePath = "./illustration.png";
$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, null, $filePath);
if($err !== null) {
$this->err = $err;
} else {
echo $cfg['domain'] . '/' . $ret['key'];
}
php向七牛上傳base64編碼的圖片
與大家分享一下我的代碼:
<?php
require_once 'vendor/autoload.php';
header('Access-Control-Allow-Origin:*');
use Qiniu\Auth;
$bucket = '要上傳的空間名';
$accessKey = '你的accessKey';
$secretKey = '你的secretKey';
$auth = new Auth($accessKey, $secretKey);
$upToken = $auth->uploadToken($bucket, null, 3600);//獲取上傳所需的token
function request_by_curl($remote_server,$post_string,$upToken) {
$headers = array();
$headers[] = 'Content-Type:image/png';
$headers[] = 'Authorization:UpToken '.$upToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$remote_server);
//curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER ,$headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$str="base64編碼的字符串";
echo "<pre>";
echo request_by_curl('http://upload.qiniu.com/putb64/-1',$str,$upToken);
echo "</pre>";
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- ThinkPHP5+UEditor圖片上傳到阿里云對(duì)象存儲(chǔ)OSS功能示例
- Thinkphp整合阿里云OSS圖片上傳實(shí)例代碼
- ThinkPHP 3使用OSS的方法
- thinkPHP簡單導(dǎo)入和使用阿里云OSSsdk的方法
- php版阿里云OSS圖片上傳類詳解
- php下通過curl抓取yahoo boss 搜索結(jié)果的實(shí)現(xiàn)代碼
- php使用curl模擬瀏覽器表單上傳文件或者圖片的方法
- php中上傳文件的的解決方案
- PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能
- PHP實(shí)現(xiàn)通過CURL上傳文件功能示例
- php實(shí)現(xiàn)表單提交上傳文件功能
- 詳解PHP使用OSS上傳文件
相關(guān)文章
Thinkphp5分頁后攜帶參數(shù)跳轉(zhuǎn)傳遞功能實(shí)現(xiàn)
這篇文章主要介紹了Thinkphp5分頁后攜帶參數(shù)進(jìn)行跳轉(zhuǎn)傳遞,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
laravel 數(shù)據(jù)驗(yàn)證規(guī)則詳解
今天小編就為大家分享一篇laravel 數(shù)據(jù)驗(yàn)證規(guī)則詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
php metaphone()函數(shù)及php localeconv() 函數(shù)實(shí)例解析
這篇文章主要介紹了php metaphone()函數(shù)及php localeconv() 函數(shù)實(shí)例解析的相關(guān)資料,需要的朋友可以參考下2016-05-05

