281 lines
6.7 KiB
PHP
281 lines
6.7 KiB
PHP
|
<?php
|
|||
|
|
|||
|
namespace taoler\com;
|
|||
|
use RecursiveIteratorIterator;
|
|||
|
use RecursiveDirectoryIterator;
|
|||
|
class Files
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>м<EFBFBD><D0BC><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
public static function create_dirs($path)
|
|||
|
{
|
|||
|
if (!is_dir($path))
|
|||
|
{
|
|||
|
$directory_path = "";
|
|||
|
$directories = explode("/",$path);
|
|||
|
array_pop($directories);
|
|||
|
|
|||
|
foreach($directories as $directory)
|
|||
|
{
|
|||
|
$directory_path .= $directory."/";
|
|||
|
if (!is_dir($directory_path))
|
|||
|
{
|
|||
|
mkdir($directory_path);
|
|||
|
chmod($directory_path, 0777);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* ɾ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>м<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param $dirPath <EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼
|
|||
|
* @param $nowDir <EFBFBD>Ƿ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>$dirPath true false
|
|||
|
*/
|
|||
|
public static function delDirAndFile( $dirPath, $nowDir=false )
|
|||
|
{
|
|||
|
if ( $handle = opendir($dirPath) ) {
|
|||
|
|
|||
|
while ( false !== ( $item = readdir( $handle ) ) ) {
|
|||
|
if ( $item != '.' && $item != '..' ) {
|
|||
|
$path = $dirPath.$item;
|
|||
|
if (is_dir($path)) {
|
|||
|
self::delDirAndFile($path.'/');
|
|||
|
rmdir($path.'/');
|
|||
|
} else {
|
|||
|
unlink($path);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
closedir( $handle );
|
|||
|
//ɾ<><C9BE><EFBFBD><EFBFBD>ǰ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
if($nowDir == true){
|
|||
|
if(rmdir($dirPath)){
|
|||
|
return true;
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>$source<63>µ<EFBFBD><C2B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ݵ<EFBFBD>$dest<73><74> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>+<EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param $source
|
|||
|
* @param $dest
|
|||
|
* @param $ex ָ<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>$source<63>µ<EFBFBD>Ŀ¼,Ĭ<EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
public function copyDirs($source, $dest, $ex=array())
|
|||
|
{
|
|||
|
$count = count($ex);
|
|||
|
if (!file_exists($dest)) mkdir($dest);
|
|||
|
if($handle = opendir($source)){
|
|||
|
while (($file = readdir($handle)) !== false) {
|
|||
|
if (( $file != '.' ) && ( $file != '..' )) {
|
|||
|
if ( is_dir($source . $file) ) {
|
|||
|
//ָ<><D6B8><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
if($count != 0){
|
|||
|
if(in_array($file,$ex)){
|
|||
|
self::copyDirs($source . $file.'/', $dest . $file.'/');
|
|||
|
}
|
|||
|
} else {
|
|||
|
self::copyDirs($source . $file.'/', $dest . $file.'/');
|
|||
|
}
|
|||
|
} else {
|
|||
|
copy($source. $file, $dest . $file);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
closedir($handle);
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼
|
|||
|
*
|
|||
|
* @param $catalogue
|
|||
|
*/
|
|||
|
public static function mkdirs($dir)
|
|||
|
{
|
|||
|
if (!file_exists($dir)) {
|
|||
|
self::mkdirs(dirname($dir));
|
|||
|
mkdir($dir, 0777);
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param $dir
|
|||
|
* @return bool
|
|||
|
* ɾ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>Լ<EFBFBD>Ŀ¼
|
|||
|
*/
|
|||
|
public static function delDir($dir) {
|
|||
|
//<2F><>ɾ<EFBFBD><C9BE>Ŀ¼<C4BF>µ<EFBFBD><C2B5>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
// var_dump(is_dir($dir));
|
|||
|
// if(!is_dir($dir)){
|
|||
|
// return true;
|
|||
|
// }
|
|||
|
$dh=opendir($dir);
|
|||
|
while ($file=readdir($dh)) {
|
|||
|
if($file!="." && $file!="..") {
|
|||
|
$fullpath=$dir."/".$file;
|
|||
|
if(!is_dir($fullpath)) {
|
|||
|
unlink($fullpath);
|
|||
|
} else {
|
|||
|
self::delDir($fullpath);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
closedir($dh);
|
|||
|
//ɾ<><C9BE><EFBFBD><EFBFBD>ǰ<EFBFBD>ļ<EFBFBD><C4BC>У<EFBFBD>
|
|||
|
if(rmdir($dir)) {
|
|||
|
return true;
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param $source
|
|||
|
* @param $dest
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|||
|
*/
|
|||
|
public static function copyDir($source, $dest)
|
|||
|
{
|
|||
|
if (!is_dir($dest)) {
|
|||
|
self::mkdirs($dest, 0755, true);
|
|||
|
}
|
|||
|
foreach (
|
|||
|
$iterator = new RecursiveIteratorIterator(
|
|||
|
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
|
|||
|
RecursiveIteratorIterator::SELF_FIRST
|
|||
|
) as $item
|
|||
|
) {
|
|||
|
if ($item->isDir()) {
|
|||
|
$sontDir = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName();
|
|||
|
if (!is_dir($sontDir)) {
|
|||
|
self::mkdirs($sontDir, 0755, true);
|
|||
|
}
|
|||
|
} else {
|
|||
|
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
/*д<EFBFBD><EFBFBD>
|
|||
|
* @param string $type 1 Ϊ<EFBFBD><EFBFBD><EFBFBD>ɿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2 ģ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
|
|||
|
public static function filePutContents($content,$filepath,$type){
|
|||
|
if($type==1){
|
|||
|
$str = file_get_contents($filepath);
|
|||
|
$parten = '/\s\/\*+start\*+\/(.*)\/\*+end\*+\//iUs';
|
|||
|
preg_match_all($parten,$str,$all);
|
|||
|
$ext_content = '';
|
|||
|
if($all[0]){
|
|||
|
foreach($all[0] as $key=>$val){
|
|||
|
$ext_content .= $val."\n\n";
|
|||
|
}
|
|||
|
}
|
|||
|
$content .= $ext_content."\n\n";
|
|||
|
$content .="}\n\n";
|
|||
|
}
|
|||
|
ob_start();
|
|||
|
echo $content;
|
|||
|
$_cache=ob_get_contents();
|
|||
|
ob_end_clean();
|
|||
|
if($_cache){
|
|||
|
$File = new \think\template\driver\File();
|
|||
|
$File->write($filepath, $_cache);
|
|||
|
}
|
|||
|
}
|
|||
|
/**
|
|||
|
* <EFBFBD><EFBFBD>ȡ<EFBFBD>ļ<EFBFBD><EFBFBD>д<EFBFBD>С
|
|||
|
*
|
|||
|
* @param string $dir <EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>
|
|||
|
* @return int
|
|||
|
*/
|
|||
|
public static function getDirSize($dir)
|
|||
|
{
|
|||
|
if(!is_dir($dir)){
|
|||
|
return false;
|
|||
|
}
|
|||
|
$handle = opendir($dir);
|
|||
|
$sizeResult = 0;
|
|||
|
while (false !== ($FolderOrFile = readdir($handle))) {
|
|||
|
if ($FolderOrFile != "." && $FolderOrFile != "..") {
|
|||
|
if (is_dir("$dir/$FolderOrFile")) {
|
|||
|
$sizeResult += self::getDirSize("$dir/$FolderOrFile");
|
|||
|
} else {
|
|||
|
$sizeResult += filesize("$dir/$FolderOrFile");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
closedir($handle);
|
|||
|
return $sizeResult;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|||
|
*
|
|||
|
* @param $files
|
|||
|
*/
|
|||
|
public static function createFile($file,$content)
|
|||
|
{
|
|||
|
|
|||
|
$myfile = fopen($file, "w") or die("Unable to open file!");
|
|||
|
fwrite($myfile, $content);
|
|||
|
fclose($myfile);
|
|||
|
return true;
|
|||
|
}
|
|||
|
/**
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>鴴<EFBFBD><EFBFBD>Ŀ¼
|
|||
|
*
|
|||
|
* @param $files
|
|||
|
*/
|
|||
|
public static function createDirOrFiles($files)
|
|||
|
{
|
|||
|
foreach ($files as $key => $value) {
|
|||
|
if (substr($value, -1) == '/') {
|
|||
|
mkdir($value);
|
|||
|
} else {
|
|||
|
file_put_contents($value, '');
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC><EFBFBD>Ŀ¼<C4BF>Ƿ<EFBFBD><C7B7><EFBFBD>д<EFBFBD><D0B4>Ȩ<EFBFBD><C8A8>
|
|||
|
public static function isWritable($file)
|
|||
|
{
|
|||
|
if (DIRECTORY_SEPARATOR == '/' AND @ ini_get("safe_mode") == FALSE) {
|
|||
|
return is_writable($file);
|
|||
|
}
|
|||
|
if (!is_file($file) OR ($fp = @fopen($file, "r+")) === FALSE) {
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
fclose($fp);
|
|||
|
return TRUE;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
|||
|
*
|
|||
|
* @param $path
|
|||
|
* @param $content
|
|||
|
* @return bool|int
|
|||
|
*/
|
|||
|
public static function writeLog($path, $content)
|
|||
|
{
|
|||
|
self::mkdirs(dirname($path));
|
|||
|
return file_put_contents($path, "\r\n" . $content, FILE_APPEND);
|
|||
|
}
|
|||
|
}
|