1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130:
<?php
namespace BaseModule\Models;
/**
* FileModel is used for working with files
*
*/
class FileModel extends BaseModel {
/**
* Current file
* @var String File name
*/
private $file;
/**
* Sets current file
* @param string $fileName
*/
public function __construct($fileName) {
$this->file = $fileName;
}
/**
* Sets file name
* @param string $file filename
*/
public function setFile($file) {
$this->file = $file;
}
/**
* Get file name (including rel. path)
* @return string
*/
public function getFile() {
return $this->file;
}
/**
* Creates (and deletes content if the file exists) the file
*/
public function createFile() {
file_put_contents($this->file, "");
}
/**
* Append content to the end of file
* @param String $data content
*/
public function appendContent($data) {
file_put_contents($this->file, $data, FILE_APPEND | LOCK_EX);
}
/**
* Return a content saved in the file
* @return String The content of the file
*/
public function getContent() {
return file_get_contents($this->file);
}
/**
* Removes file
*/
public function deleteFile() {
unlink($this->file);
}
/**
* Removes content
*/
public function removeContent() {
$this->createFile();
$this->deleteFile();
$this->createFile();
}
/**
* Deletes all old files (older than a week) from the directory
*
* @param string $path directory path
* @return boolean returns FALSE just in case the directory doesn't exist
*/
public static function deleteOldFiles($path) {
if (substr($path, -1) != DIRECTORY_SEPARATOR) {
$path .= DIRECTORY_SEPARATOR;
}
if (!file_exists($path) || !is_dir($path)) {
$return = false;
} else {
$files = scandir($path);
foreach ($files as $f) {
if ($f == "." || $f == "..") {
continue;
}
$timeDif = time() - filemtime($path . $f);
if ($timeDif > 7 * 24 * 60 * 60) {
$deleteFile = new FileModel($path . $f);
$deleteFile->deleteFile();
}
}
$return = true;
}
return $return;
}
/**
* Deletes dir completely, including it's content
*
* @param string $dir path to directory to delete
* @return bool true on successful deletion
*/
public static function deleteDir($dir) {
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file) {
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
return rmdir($dir);
}
}