file($this->FileName);
//$FileInfo->close();
return $Result;
}
function GetSize($Item): int
{
$FileName = $this->GetFullName($Item);
if (file_exists($FileName)) $Result = filesize($FileName);
else $Result = 0;
return $Result;
}
function GetFullName(): string
{
$Path = '';
$ParentId = $this->Directory;
while ($ParentId != null)
{
$DbResult = $this->Database->select('FileDirectory', '*', 'Id='.$ParentId);
$DbRow = $DbResult->fetch_assoc();
$Path .= $DbRow['Name'].'/';
$ParentId = $DbRow['Parent'];
}
$Result = $this->UploadFileFolder.'/'.$Path.$this->FileName;
return $Result;
}
function GetExt(): string
{
return substr($this->Name, 0, strpos($this->Name, '.') - 1);
}
function Delete(): void
{
if (file_exists($this->GetFullName())) unlink($this->GetFullName());
}
function GetContent(): string
{
if ($this->TempName != '') $Content = file_get_contents($this->TempName);
else $Content = file_get_contents($this->GetFullName());
return $Content;
}
}
class TypeFile extends TypeBase
{
public string $UploadFileFolder;
public string $FileDownloadURL;
public string $DirectoryId;
function __construct(FormManager $FormManager)
{
parent::__construct($FormManager);
$this->FileDownloadURL = 'file';
$this->DirectoryId = null;
}
function OnView(array $Item): ?string
{
$File = &$Item['Value'];
return ''.
$File.' ('.HumanSize($File->Size).')';
}
function OnEdit(array $Item): string
{
// Check max value of upload_max_filesize
// ini_set("upload_max_filesize", "100M");
//
$File = &$Item['Value'];
$Output = '';
return $Output;
}
function OnLoad(array $Item): ?string
{
if (!is_object($Item['Value'])) $Item['Value'] = new DbFile();
$File = &$Item['Value'];
if (array_key_exists($Item['Name'], $_FILES) and ($_FILES[$Item['Name']]['name'] != ''))
{
$UploadFile = $_FILES[$Item['Name']];
if (file_exists($UploadFile['tmp_name']))
{
$File->Name = $UploadFile['name'];
$File->Size = $UploadFile['size'];
$File->TempName = $UploadFile['tmp_name'];
}
}
return $File;
}
function OnLoadDb(array $Item): ?string
{
if (!is_object($Item['Value'])) $Item['Value'] = new DbFile();
$File = &$Item['Value'];
$DbResult = $this->Database->select('File', '*', 'Id='.$File->Id);
if ($DbResult->num_rows > 0)
{
$DbRow = $DbResult->fetch_assoc();
$File->Name = $DbRow['Name'];
$File->Size = $DbRow['Size'];
$File->Directory = $DbRow['Directory'];
}
return $File;
}
function OnSaveDb(array $Item): ?string
{
if (!is_object($Item['Value'])) $Item['Value'] = new DbFile();
$File = &$Item['Value'];
$Properties = array('Name' => $File->Name,
'Size' => $File->GetSize(), 'Directory' => $File->Directory, 'Hash' => 'SHA1(CONCAT(Id,Name,Size,Time))');
$DbResult = $this->Database->select('File', '*', 'Id='.$File->Id);
if ($DbResult->num_rows > 0)
{
$DbRow = $DbResult->fetch_assoc();
if ($File->TempName != '')
{
$FileName = $File->GetFullName();
unlink($FileName);
}
$this->Database->update('File', 'Id='.$File->Id, $Properties);
} else
{
$this->Database->insert('File', $Properties);
$File->Id = $this->Database->insert_id;
}
if (!move_uploaded_file($File->TempName, $FileName))
$this->System->SystemMessage('Nahrání souboru', 'Cílová složka není dostupná!');
return '';
}
}