class YLFile
{
private static $_instance;
private $directory = [];
function __construct() { }
public static function instance() {
if (!(self::$_instance instanceof self))
self::$_instance = new self;
return self::$_instance;
}
public function __clone() {
trigger_error('Clone is not allow', E_USER_ERROR);
}
public function readFileContent($path) {
if (is_file($path))
$content = file_get_contents($path);
else
$content = '';
if ($content === false)
return ['code' => 0, 'msg' => '获取内容失败'];
else
return ['code' => 1, 'msg' => $content];
}
public function writeFileContent($path, $content) {
$flag = 0;
$msg = '保存失败';
$baseDir = dirname($path);
if (!is_dir($baseDir))
mkdir($baseDir);
if (!is_file($path))
touch($path);
$flag = file_put_contents($path, $content);
$flag && $msg = '保存成功' && $flag = 1;
return ['code' => $flag, 'msg' =>$msg];
}
public function checkFileOrDirWithPath($path, $directory = '') {
$flag = 0;
$msg = '文件存在,请重新输入';
if (!is_file($path))
$flag = 1;
if ($flag) {
$filename = pathinfo($path, PATHINFO_FILENAME);
$msg = ['directory' => $directory, 'filename' => $filename];
}
return ['code' => $flag, 'msg' => $msg];
}
public function deleteFileOrDirWithPath($path) {
is_file($path) && unlink($path);
$files = scandir(dirname($path));
$flag = 0;
$msg = '删除失败';
if ( count($files) === 2 )
$flag = $this->_delDir(dirname($path));
$flag && $msg = '删除成功';
return ['code' => $flag, 'msg' => $msg];
}
private function _delDir($dir) {
$dh = opendir($dir);
while ($file = readdir($dh)) {
if ($file != "." && $file != "..") {
$fullPath = $dir . DIRECTORY_SEPARATOR . $file;
if (!is_dir($fullPath)) {
unlink($fullPath);
} else {
$this->delDir($fullPath);
}
}
}
closedir($dh);
if (rmdir($dir))
return 1;
else
return 0;
}
public function getAllDirectoryOrNode($baseDir, $type = 'directory') {
if ($type === 'directory') {
return $this->_allDir($baseDir);
} elseif ($type === 'node') {
$this->directory = [];
$this->_allNodes($baseDir);
return $this->directory;
} else {
return null;
}
}
private function _allDir($baseDir) {
$files = scandir($baseDir);
$dirs = [];
foreach ($files as $file) {
if ($file === '.' || $file === '..')
continue;
$subPath = $baseDir . DIRECTORY_SEPARATOR . $file;
if (is_dir($subPath))
$dirs[] = $file;
}
return $dirs;
}
private function _allNodes($baseDir, $subDir = '', $ext = 'html') {
$files = scandir($baseDir);
foreach ($files as $file) {
if ($file === '.' || $file === '..')
continue;
$subPath = $baseDir . DIRECTORY_SEPARATOR . $file;
if (is_dir($subPath)) {
$this->directory[$file] = [];
$this->_allNodes($subPath . DIRECTORY_SEPARATOR, $file);
} else if (is_file($subPath) && pathinfo($file, PATHINFO_EXTENSION) === $ext) {
$filename = pathinfo($subPath, PATHINFO_FILENAME);
if (empty($subDir))
$this->directory[] = $filename;
else
$this->directory[$subDir][] = $filename;
}
}
}
}