<?php
namespace ticky\helper;
class file {
public static $map = [];
public static function getInfo($file, $field = '') {
runlog('error', 'ticky\helper\file\getInfo:' . $file);
$info = [];
if (!self::exists($file)) {
return false;
}
$handle = new \SplFileInfo($file);
$info['size'] = $handle->getSize();
$info['type'] = $handle->getType();
$info['extension'] = strtolower($handle->getExtension());
$info['base_path'] = $handle->getPath();
$info['file_path'] = $handle->getRealPath();
$info['base_name'] = $handle->getBasename('.' . $handle->getExtension());
$info['file_name'] = $handle->getFilename();
$info['group'] = $handle->getGroup();
$info['owner'] = $handle->getOwner();
$info['auth'] = $handle->getPerms();
$info['last_access_time'] = date('Y-m-d H:i:s', $handle->getATime());
$info['last_modify_time'] = date('Y-m-d H:i:s', $handle->getMTime());
$info['isExecutable'] = $handle->isExecutable();
$info['isReadable'] = $handle->isReadable();
$info['isWritable'] = $handle->isWritable();
return $field ? $info[$field] : $info;
}
public static function getSize($file) {
return self::getInfo($file, 'extension');
}
public static function exists($file) {
return file_exists($file);
}
public static function load($file, $data = null) {
if (empty($file) || !isset($file)) {
return false;
}
$key = base64_encode($file);
if (!self::exists($file)) {
return false;
}
if (!is_null($data)) {
extract($data, EXTR_OVERWRITE);
}
if (!self::$map[$key]) {
self::$map[$key] = $file;
include $file;
}
return true;
}
public static function create($file, $data = null) {
$str = '';
if (is_array($data)) {
foreach ($data as $k => $v) {
$str .= $k . " : " . $v . " ";
}
} else {
$str = $data . "\r\n";
}
$path = dirname($file);
dir::exists($path) or dir::create($path);
if (file_put_contents($file, $str, FILE_APPEND)) {
return true;
}
return false;
}
public static function delete($file) {
return self::exists($file) ? unlink($file) : false;
}
public static function move($oldFile, $newFile) {
return self::exists($oldFile) ? rename($oldFile, $newFile) : false;
}
public static function copy($oldFile, $newFile) {
return self::exists($oldFile) ? copy($oldFile, $newFile) : false;
}
public static function writelog($file, $log) {
$yearmonth = date('Ym');
$logdir = LOG_PATH . DS;
dir::exists($logdir) or dir::create($logdir);
$logfile = $logdir . $yearmonth . '_' . $file . '.php';
if (@filesize($logfile) > 2048000) {
$dir = opendir($logdir);
$length = strlen($file);
$maxid = $id = 0;
while ($entry = readdir($dir)) {
if (strpos($entry, $yearmonth . '_' . $file) !== false) {
$id = intval(substr($entry, $length + 8, -4));
$id > $maxid && $maxid = $id;
}
}
closedir($dir);
$logfilebak = $logdir . $yearmonth . '_' . $file . '_' . ($maxid + 1) . '.php';
@rename($logfile, $logfilebak);
}
if ($fp = @fopen($logfile, 'a')) {
@flock($fp, 2);
if (!is_array($log)) {
$log = array($log);
}
foreach ($log as $tmp) {
fwrite($fp, "<?PHP exit;?>\t" . str_replace(array('<?', '?>'), '', $tmp) . "\n");
}
fclose($fp);
}
}
}