需要安装composer 扩展包
composer require xiflys/jiguangfile=dev-main
<?php
/**
* TbFile.php
* 文件描述
* created on 21:13 2021/2/9 21:13
* create by xiflys
*/
require './vendor/autoload.php';
class TbFile
{
/**
* Undocumented function
*
* @param [type] $dir 文件夹名
* @param [type] $fname 文件数据记录文件名
*/
public function __construct($dir, $fname)
{
$this->demofile($dir, $fname);
}
protected function demofile($dir, $fname)
{
if (!file_exists($fname)) {
try {
$fp = fopen($fname, "w+");
fclose($fp);
chmod($fname, 0777);
} catch (\Throwable $th) {
//throw $th;
print_r($th->getMessage());
die;
}
}
$ars = explode(',', trim(file_get_contents($fname), ','));
$handle = opendir($dir);
try {
while ($line = readdir($handle)) {
if ($line == '.' || $line == '..') {
continue;
}
$ars = explode(',', trim(file_get_contents($fname), ','));
if (file_exists($dir . '/' . $line) && !is_dir($dir . '/' . $line)) {
chmod($dir . '/' . $line, 0777); # 这里 一般静态文件777 也无所谓了
if (!in_array($dir . '/' . $line, $ars)) {
$dis = $dir . '/' . $line;
$info = $this->upload('/cdn/xms'.$dis,$dis);
if($info['hash'] == ''){
continue;
}
// dump($as);die;
# 文件 就是 $dir.'/'.$line
chmod($fname, 0777);
if(!isset($ags['message'])){
echo $dis.'同步成功'."<br>";
}
# 写入文件 数据
$fp = fopen($fname, "a+");
fwrite($fp, ',' . $dir . '/' . $line);
fclose($fp);
#file_put_contents($fname,file_get_contents($fname).','.$dir . '/' . $line);
}
}
if (is_dir($dir . '/' . $line)) {
new self($dir . '/' . $line, $fname);
}
}
} catch (\Throwable $th) {
#print_r($th->getMessage());exit;
}
}
protected function base64jm($filename){
return base64_encode(file_get_contents($filename));
}
public function upload($cloudname,$filename){
$qiniu = \xiflys\jiguangfile\QiniuDrive::getInstance();
// 网盘路径本地路径
$info = $qiniu->upload($cloudname,$filename);
print_r($info);
return $info;
}
}
# 更改一下参数
$dir = 'js'; #需要同步的文件夹名
$FileRecord = '1.txt'; #随便定义一个文件名 用来记录数据
# 下面无需更改
try {
//code...
$arr = new TbFile($dir, $FileRecord);
} catch (\Throwable $th) {
//throw $th;
print_r($th->getmessage());
die;
}