PHP demo
删除的时候记住不要带域名,也不要带'/'这个路径符号,如全路径是:'https://hxty.oss-cn-beijing.aliyuncs.com/data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf',则这里的path_file路径为:'data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf'。
<?php
include './aliyun-oss-php-sdk-2.4.2/autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
error_reporting(E_ALL);
ini_set('display_errors', 1); //错误信息
ini_set('display_startup_errors', 1); //php启动错误信息
session_start();
/**
* 取小数点后两位
* @param type $str 带小数的数字
* @return type 返回准确截取两位的小数
*/
function num_format($str) {
if (strpos($str, '.') === false) {
return $str;
}
$retArr = explode(".", $str);
$strs = substr($retArr[1], 0, 2);
return $retArr[0] . "." . $strs;
}
function create_folders($dir) {
return is_dir($dir) or ( create_folders(dirname($dir)) and mkdir(iconv("UTF-8", "GBK", $dir), 0777, true));
}
$file = $_FILES['file'];
$accessKeyId = '';//RAM alioss后台能拿到
$accessKeySecret = '';//RAM alioss后台能拿到
$endpoint = 'oss-cn-beijing.aliyuncs.com';//alioss后台 概览可以拿到
$bucket = 'planet-cross';//创建的组,随便吧创建,权限正常就行
echo '<pre>';
var_dump($_SESSION);
if ($file) {
$filename = "/upload/image/" . date("Ymd");
if (!file_exists("." . $filename)) {
create_folders("." . $filename);
}
//获取后缀
$ary = explode('.', $file['name']);
$su = end($ary);
//文件名
$filenames = $filename . "/" . time() . rand(1, 10000) . "." . $su;
move_uploaded_file($file['tmp_name'], "." . $filenames);
$ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
$filenames = ltrim($filenames, '/'); //去除 第一个 '/'
// 开始上传
$pic = $filenames;
try {
$oss = $ossClient->uploadFile($bucket, $pic, $filenames);
$filenames = $oss['info']['url'];
$_SESSION['file']=$filenames;
echo $filenames;
echo '<img src="' . $filenames . '" />';
//删除本地文件
} catch (\OSS\Core\OssException $e) {
die("Failed to upload file ");
return false;
}
} else if(!empty($_SESSION['file'])){
try {
$patch=trim(parse_url($_SESSION['file'])['path'],'/');
$ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
$oss=$ossClient->deleteObject($bucket,$patch);
$_SESSION['file']=null;
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
}
/**
* 删除文件
* 可以删除调用ajaxfile上传的文件
* @param type $path_file 文件路径
* 删除的时候记住不要带域名 也不要带 / 这个路径符号
*/
function delfile($path_file) {
if (config('config.OSS_OPEN')) {
try {
// 删除的时候记住不要带域名,也不要带'/'这个路径符号,如全路径是:'https://hxty.oss-cn-beijing.aliyuncs.com/data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf',则这里的path_file路径为:'data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf'。
$path_file=trim(parse_url($path_file)['path'],'/');//去掉域名,
$ossClient = new \OSS\OssClient(config('config.OSS_KEY'), config('config.OSS_SECRET'), config('config.OSS_ENDPOINT'));
$ossClient->deleteObject(config('config.OSS_BUCKET'), $path_file);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
} else {
$val2 = "." . $path_file;
if (file_exists($val2)) {
$unlinkfilename = trim($path_file, "/");
unlink($unlinkfilename);
}
}
}
删除的时候记住不要带域名,也不要带'/'这个路径符号,如全路径是:'https://hxty.oss-cn-beijing.aliyuncs.com/data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf',则这里的path_file路径为:'data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf'。
|