You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
think-library/src/helper/Files.php

186 lines
5.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace DtApp\ThinkLibrary\helper;
use DtApp\ThinkLibrary\exception\DtaException;
use think\Exception;
use ZipArchive;
/**
* 文件管理类
* @mixin Files
* @package DtApp\ThinkLibrary\helper
*/
class Files
{
/**
* 删除文件
* @param string $name 路径
* @return bool
* @throws DtaException
*/
public function delete(string $name): bool
{
if (empty($name)) {
throw new DtaException('请检查需要删除文件夹的名称');
}
return file_exists($name) && unlink($name);
}
/**
* 删除文件夹
* @param string $name 路径
* @return bool
* @throws DtaException
* @throws Exception
*/
public function deletes(string $name): bool
{
if (empty($name)) {
throw new DtaException('请检查需要删除文件夹的名称');
}
//先删除目录下的文件:
$dh = opendir($name);
while ($file = readdir($dh)) {
if ($file !== "." && $file !== "..") {
$fullpath = $name . "/" . $file;
if (!is_dir($fullpath)) {
unlink($fullpath);
} else {
$this->deletes($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if (rmdir($name)) {
return true;
}
return false;
}
/**
* 把文件夹里面的文件打包成zip文件
* @param string $name 路径
* @param string $suffix_name 需要打包的后缀名,默认.png
* @param string $file_name 文件名,默认全部名
* @return bool
* @throws DtaException
*/
public function folderZip(string $name, string $suffix_name = '.png', string $file_name = '*'): bool
{
if (empty($name)) {
throw new DtaException('请检查需要打包的路径名称');
}
$list = glob($name . "{$file_name}.{$suffix_name}");
$fileList = $list;
$zip = new ZipArchive();
// 打开压缩包
$zip->open($name, ZipArchive::CREATE);
//向压缩包中添加文件
foreach ($fileList as $file) {
$zip->addFile($file, basename($file));
}
//关闭压缩包
$zip->close();
return true;
}
/**
* 获取目录下的所有文件和目录
* @param string $path
* @return array
*/
public function getFiles(string $path): array
{
$files = [];
if (is_dir($path)) {
$path = dirname($path) . '/' . basename($path) . '/';
$file = dir($path);
while (false !== ($entry = $file->read())) {
if ($entry !== '.' && $entry !== '..') {
$cur = $path . $entry;
if (is_dir($cur)) {
$subPath = $cur . '/';
$this->getFiles($subPath);
}
$files[] = $cur;
}
}
$file->close();
return $files;
}
return [];
}
/**
* 删除目录下的文件
* @param string $path
* @return bool
*/
public function rmFiles(string $path): bool
{
$files = $this->getFiles($path);
if (!is_array($files)) {
return false;
}
if (empty($files)) {
return false;
}
foreach ($files as $item => $file) {
if (is_dir($file)) {
rmdir($file);
} elseif (is_file($file)) {
unlink($file);
}
}
return true;
}
/**
* 判断文件是否存在
* @param string $path
* @return bool
*/
public function judgeFile(string $path): bool
{
if (file_exists($path)) {
return true;
}
return false;
}
/**
* 判断目录是否存在
* @param string $path
* @return bool
*/
public function judgeContents(string $path): bool
{
if (is_dir($path)) {
return true;
}
return false;
}
}