nulib/php/src_file/base/_File.php

36 lines
1.0 KiB
PHP

<?php
namespace nulib\file\base;
use nulib\os\IOException;
use nulib\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;
/** @return resource */
protected function open() {
return IOException::ensure_valid(@fopen($this->file, $this->mode));
}
/** @return resource */
function getResource() {
if ($this->fd === null && $this->file !== null) $this->fd = $this->open();
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;
}
}