phore / filesystem
Acces files layer
Requires
- php: >8.1
- ext-json: *
- ext-yaml: *
- phore/core: *
- psr/http-message: ^1.0@dev
Requires (Dev)
- phpunit/phpunit: ^9.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 11:13:25 UTC
README
File access functions
Version 2
Version 2 requires PHP 8.5 or newer and introduces explicit filesystem security policies. New filesystem entry points follow symbolic links by default, matching normal filesystem behavior. With rootDir set, resolved symlink targets must stay inside that root. Root-bound objects carry their security context through derived files, directories, walks and streams.
The current v1 release line remains v1.1.x; this pull request prepares v2.0.0.
- Working with sub-paths
- Checking symbolic links
Installation
composer require phore/filesystem:^2.0
Security-first usage
$root = phore_dir('/srv/site/docs', [ 'rootDir' => '/srv/site/docs', ]); $page = $root->withSubPath('index.md')->asFile(); echo $page->get_contents();
The default is followSymlinks=true. Set rootDir when access must stay inside a bounded tree; symlinks may then be followed only when their resolved target remains inside that root. Use followSymlinks=false for an explicit no-symlink policy.
General usage
echo phore_uri("/tmp/some.file")->withDirName();
will result in
/tmp
Subpath
echo phore_uri("/tmp")->withSubPath("./some/other/file")
/tmp/some/other/file
Assertions
phore_uri("/tmp")->assertIsFile()->assertIsWritable();
Ownership and write permissions
The following ownership methods belong to PhoreFile, not PhoreUri.
isOwnedBy(?int $uid = null): boolchecks whether an existing path belongs to the given UID.assertOwnedBy(?int $uid = null): selfperforms the same check and returns the object for fluent usage. On an ownership mismatch, it throwsFileAccessExceptionwith the path, expected UID, actual owner UID and octal permissions.- With
null, both methods use the effective process UID fromposix_geteuid(), not the PHP script owner's UID. If POSIX support is unavailable, pass an explicit UID; automatic UID detection otherwise throws an exception.
Both methods respect the bound filesystem restrictions. Missing or inaccessible paths can throw filesystem exceptions rather than returning false.
Ownership and writability are independent: a file owned by another user may still be writable. Use isOwnedBy() to decide whether to attempt an optional chmod(); skip that permission change for foreign-owned files without preventing permitted writes. Use assertOwnedBy() only when foreign ownership is an actual error. Ownership alone does not guarantee that chmod() will succeed.
PhoreFile::assertWritable() reports the path, octal permissions, owner UID, effective process UID and whether the process user owns the path when it is not writable. The actual write operation can still fail after a successful check; these checks do not provide atomic guarantees against concurrent filesystem changes.
Reading YAML
phore_uri("/tmp/somefile.yml")->assertFile()->get_yaml();
Front matter files
Jekyll-style YAML front matter can be read together with the remaining content:
$document = phore_file("post.md")->get_front_matter(); echo $document->header["title"]; echo $document->content;
Pass a class name to hydrate the header when phore/hydrator is installed:
$document = phore_file("post.md")->get_front_matter(PostHeader::class); echo $document->header->title;
Create or replace a front matter file with put_front_matter():
phore_file("post.md")->put_front_matter( new \Phore\FileSystem\FrontMatterFile(null, ["title" => "Example"], "Content") );
Tempoary Files
Will be unlinked when object destructs.
$file = new PhoreTempFile();