删除的时候记住不要带域名,也不要带'/'这个路径符号,如全路径是:'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();
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;
}
}
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'。
到此这篇关于php 文件上传至OSS及删除远程阿里云OSS文件的文章就介绍到这了,更多相关php删除阿里云OSS文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!