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:
<?php
namespace DispatchModule\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, "");
}
/**
* Content that will be added to the end of file
* @param String $data content
*/
public function appendContent($data) {
file_put_contents($this->file, $data, FILE_APPEND | LOCK_EX);
}
/**
* Return the file 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;
}
$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;
}
}