deleteObjects($bucket, $object);" and other methods."/> deleteObjects($bucket, $object);" and other methods.">
Home > Article > Backend Development > How to delete files in php oss
php How to delete files in oss: first download and install the sdk in Alibaba Cloud; then delete the files through methods such as "$ossClient->deleteObjects($bucket, $object);".
Recommended: "PHP Video Tutorial"
oss file upload and deletion (batch deletion) processing
The blogger uses Alibaba Cloud's oss
First download and install the SDK on Alibaba Cloud. Please download the relevant SDK from Alibaba Cloud by yourself
Document address https://help.aliyun.com/document_detail/85580.html?spm=a2c4g.11174283.6.1006.55ad7da2hNKC0w#h2-url-2
The blogger used the third method
<?php require_once './aliyun-oss-php-sdk-master/autoload.php'; header("content-type:text/html;charset=utf-8"); // if (is_file(__DIR__ . '/aliyun-oss-php-sdk-master/autoload.php')) { // require_once __DIR__ . '/aliyun-oss-php-sdk-master/autoload.php'; // } if (is_file(__DIR__ . '/aliyun-oss-php-sdk-master/autoload.php')) { require_once __DIR__ . '/aliyun-oss-php-sdk-master/autoload.php'; } if (is_file(__DIR__ . '/aliyun-oss-php-sdk-master/vendor/autoload.php')) { require_once __DIR__ . '/aliyun-oss-php-sdk-master/vendor/autoload.php'; } use OSS\OssClient; use OSS\Core\OssException; // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 $accessKeyId = "xxxxx"; $accessKeySecret = "xxxxx"; // Endpoint以杭州为例,其它Region请按实际情况填写。 $endpoint = "http://oss-cn-shenzhen.aliyuncs.com"; // 存储空间名称 $bucket = "xxxx"; $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 判断bucketname是否存在,不存在就去创建 if (!$ossClient->doesBucketExist($bucket)) { $ossClient->createBucket($bucket); } // 文件名称 $object = $_FILES['filename']['name']; // <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt $filePath = $_FILES['filename']['tmp_name']; try{ //上传图片 $ossClient->uploadFile($bucket, $object, $filePath); //文件访问权限,设置为所有人可读 $acl = "public-read"; $ossClient->putObjectAcl($bucket, $object, $acl); echo "<img src ='http://xxxx.oss-cn-shenzhen.aliyuncs.com/".$object."' >";die; //删除单个文件 // $ossClient->deleteObjects($bucket, $object); //删除多个文件 // $objects = ['文件名1','文件名2']; // $ossClient->deleteObjects($bucket, $objects); } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");rrree
The above is the detailed content of How to delete files in php oss. For more information, please follow other related articles on the PHP Chinese website!