Files
thetool/application/File/File.php
2024-08-01 20:45:44 +02:00

116 lines
2.6 KiB
PHP

<?php
class File extends mfBaseModel {
private $creator;
private $editor;
public static function createFile($filename = false, $overwrite = false) {
// make filename
}
protected function beforeSave() {
if($this->mimetype) {
return true;
}
$this->mimetype = $this->getMimetype();
}
public function getMimetype() {
if(!$this->store_filename) return false;
$filepath = $this->getFullPath();
if(!file_exists($filepath)) return false;
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($filepath);
return $mime;
}
public function delete() {
if($this->id) {
$id = $this->id;
// delete file in store
if($this->subfolder) {
$path = MFUPLOAD_FILE_SAVE_PATH."/".$this->subfolder."/".$this->store_filename;
} else {
$path = MFUPLOAD_FILE_SAVE_PATH."/".$this->store_filename;
}
if(!unlink($path)) {
$this->log->warn(__CLASS__."::delete(): Error unlinking file ($path)");
}
$where = "id=$id";
if($this->fieldprefix && !strstr($field,"_")) {
$where = $this->fieldprefix."_id=$id";
}
if($this->db->delete($this->table,$where)) {
if(method_exists($this, "afterDelete")) {
$this->afterDelete();
}
$this->data = new stdClass();
$this->id = "";
return true;
}
}
return false;
}
public function getFullPath() {
/*if(!is_numeric($this->id) || $this->id < 1) {
throw new Exception("File not found", 4040);
}*/
$filename = $this->store_filename;
$path = MFUPLOAD_FILE_SAVE_PATH;
$path .= ($this->subfolder) ? "/".$this->subfolder : "";
$path .= "/$filename";
if(!file_exists($path)) {
throw new Exception("File not found", 4041);
}
return $path;
}
public function getContents() {
$outname = $this->getFullPath();
return file_get_contents($outname);
}
public function getProperty($name) {
if($this->$name == null) {
if(!$this->id) {
return null;
}
if($name == "creator") {
$this->creator = new User($this->create_by);
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}