公共部分:
<?php
/**
* User: xahy
* Description: 公共部分
*/
require __DIR__.'/vendor/autoload.php';
use Upyun\Upyun as UpYunObj;
use Upyun\Config as UpYunConfig;
/****
* 链接又拍云服务
* @param string $serverName 服务名称
* @param string $serverUser 用户
* @param string $serverPwd 密码
*/
$serverName = '';
$serverUser = '';
$serverPwd = '';
$serviceConfig = new UpYunConfig($serverName, $serverUser, $serverPwd);
$client = new UpYunObj($serviceConfig);
标题清空内容脚本:
<?php
/**
* User: xahy
* Description: 清空指定目录下所有文件和目录,默认根目录
*/
require_once __DIR__."/in.php";
/***
* 当指定目录存储使用量比较大的时候,请打开以下注释取消运行时间限制和声明后台运行。
* 如果想在linux上持续执行,请使用命令: nohup php -f /home/wwwroot/del/loo.php &
*/
//后台运行
//ignore_user_abort();
//取消脚本运行时间的超时上限
//set_time_limit(0);
///要删除的目录
$path = "/";
echo "<pre>";
try{
if($client->getMimetype($path) != "application/octet-stream"){
var_dump("rm message:请填写正确的目录");
die;
}
while(! empty($client->read($path)["files"])){
c($client,$path);
}
var_dump("rm message:success");
}catch(\Exception $e){
var_dump($e->getMessage());
die;
}
/****
* User: xahy
* Description: 递归删除目录or文件
* @param $client
* @param string $path
* @param string $f
*/
function c($client,$path = "/",$f = "F"){
$res = $client->read($path);
$files = $res["files"];
///如果读取内容为空且是目录,则删除此目录
if(empty($files) && $f == "F" ){
$client->delete($path,true);
}else{
///遍历读取出来的内容,如果是文件则删除当前文件,如果是目录则遍历此目录下内容
foreach($files as $k=>$v){
$v_type = $v["type"];
$v_name = $v["name"];
$actionPath = substr($path, -1) == "/" ? $path.$v_name : $path."/".$v_name;
if($v_type == "N"){
$client->delete($actionPath,true);
}else if($v_type == "F"){
c($client,$actionPath,$v_type);
}
}
}
}
查看清理状态脚本:
<?php
/**
* User: xahy
* Description: 查看使用量,默认根目录
*/
require_once __DIR__."/in.php";
echo "<pre>";
$files = $client->read("/")['files'];
if(empty($files)){
var_dump("check result:清理已完成");
}else{
var_dump("清理进行中....");
var_dump("整个云服务使用空间大小,参考值:" . BytesTo($client->usage("/")));
}
完整代码 gitee 地址:https://gitee.com/xianhenyuan/rm-up-content