TP5 file upload SDK qiniu cloud

When you're lonely, you'll be accompanied by a dog 2022-02-13 06:25:41 阅读数:592

tp5 tp file upload sdk

1、 Use composer Command to install Qianniu cloud plug-in

         composer require qiniu/php-sdk

 2、 Go to qiniu cloud to register your account , Real name authentication

  1. Log in after registering your account , After the real name authentication is completed, it can be in , Click on your avatar , In the personal center, you can get AK,sk Convenient for us to use later ;

 2. Create object storage space

3、 Code

 function icloud()
{
// Receive file upload value
$file = $_FILES;
// Local file path
$localFilePath = $file['front_photo']['tmp_name'];
// Truncate the file suffix
//$suffix = '.jpg';
$suffix = strtolower(substr($file['file']['name'],strpos($file['file']['name'],'.')));
// Generate a unique file name , rename (md5 Encrypt original file name + second + random number )
$fileName = md5($file['file']['name']) . date('s',time()) . rand(1,9999999);
$fileName .= $suffix;
// Upload qiniu cloud business logic
$accessKey = ''; // Go to the key management of the console and get AK
$secretKey = '';// Go to the key management of the console and get SK
$auth = new Auth($accessKey, $secretKey);
// Seven cattle cloud bucket name , Fill in according to your actual situation
$bucket = '';// Fill in the name of the object storage space created by yourself
// Generate upload Token
$token = $auth->uploadToken($bucket);
// structure UploadManager object
$uploadMgr = new UploadManager();
// call UploadManager Of putFile Method to upload the file .
list($ret, $err) = $uploadMgr->putFile($token, $fileName, $localFilePath);
// Error message prompt
if ($err != null) {
// Can be adjusted to an error page
$this->error(' Failed to upload file ');
}
$imageUrl = 'http://qtpud69oi.hn-bkt.clouddn.com/' . $fileName;
// Receipt business logic create save Only these two model methods can automatically write timestamps
$result =BannerModel::create([
'image_url'=>$imageUrl,
]);
$this->success(' Upload file successful ');
// Store the image path of qiniu cloud into our own database Seven cattle cloud picture path
$imageUrl = 'http://qtpud69oi.hn-bkt.clouddn.com/' . $fileName;// Domain name stitching image name is the path to the image cloud
}

4、 Pictures uploaded to the cloud successfully

  Click the details to find the picture , And the cloud path of the picture

copyright:author[When you're lonely, you'll be accompanied by a dog],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130625385655.html