54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
namespace app\service;
|
|
|
|
class Tool
|
|
{
|
|
//删除文件
|
|
public static function delFile($path)
|
|
{
|
|
if(!empty(trim($path))){
|
|
$realPath = app()->getRootPath() . ltrim($path, '/');
|
|
if (file_exists($realPath)) {
|
|
$info = pathinfo($realPath);
|
|
$source = $info['dirname'] . "/" . $info['filename'] . '*.' . $info['extension'];
|
|
foreach(glob($source) as $filename){
|
|
if(is_file($filename)){
|
|
unlink($filename);
|
|
}
|
|
}
|
|
clearstatcache();// 清除缓存
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除目录下的所有文件和子目录
|
|
* 调用完毕后请用clearstatcache()清理缓存
|
|
*/
|
|
public static function removeByPath($path)
|
|
{
|
|
if(is_dir($path)) {
|
|
if($handle = @opendir($path)) {
|
|
while (($file = readdir($handle)) !== false){
|
|
if($file != '.' && $file != '..') {
|
|
$child = $path.'/'.$file;
|
|
is_dir($child) ? self::removeByPath($child) : @unlink($child);
|
|
}
|
|
}
|
|
}
|
|
closedir($handle);
|
|
} elseif (is_file($path)) {
|
|
@unlink($path);
|
|
} else {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
//去除字符串空格
|
|
public static function trimSpace($str)
|
|
{
|
|
return str_replace(' ', '', trim($str));
|
|
}
|
|
}
|