nur-sery/src/os/file/_File.php

34 lines
1002 B
PHP
Raw Normal View History

2023-12-31 22:28:03 +04:00
<?php
namespace nur\sery\os\file;
use nur\sery\os\IOException;
use nur\sery\web\http;
abstract class _File extends Stream {
function __construct($fd, bool $close, bool $throwOnError=true, ?bool $allowLocking=null) {
parent::__construct($fd, $close, $throwOnError, $allowLocking);
}
/** @var string */
protected $file;
/** @var string */
protected $mode;
2024-01-01 18:14:49 +04:00
protected function open() {
return IOException::ensure_value(@fopen($this->file, $this->mode));
}
2023-12-31 22:28:03 +04:00
function getResource() {
2024-01-01 18:14:49 +04:00
if ($this->fd === null && $this->file !== null) $this->fd = $this->open();
2023-12-31 22:28:03 +04:00
return parent::getResource();
}
/** streamer le contenu du fichier en sortie */
function readfile(?string $contentType=null, ?string $charset=null, ?string $filename=null, string $disposition=null): bool {
if ($contentType !== null) http::content_type($contentType, $charset);
if ($filename !== null) http::download_as($filename, $disposition);
return readfile($this->file) !== false;
}
}