Core\IO::isWritable
Checks whether the program may write to what is at the path.
Core\IO::isWritable returns true when the operating system allows this program to change the file, or to create files in the folder, at the path. It returns false when it does not. It also returns false when there is nothing at the path.
This means a file you are about to create is not writable yet. To check that you can create a new file, pass the folder it goes into.
The answer is true at the moment of the check. A Core\IO::write that follows can still throw an IOError.
A program you start from the command line may write any path. On a server, a program may write only in the folders that Core\IO::createTemporaryDirectory made for it. Any other path needs the fs.write capability. Without it, isWritable throws a RuntimeError, even when the program may read the path.
Parameters
$pathstringThe file or directory to check. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.
Return value
Returns bool. true if the operating system would allow a write, false if it would not. A path where nothing exists gives false. As with isReadable, the result is only true at the moment of the call.
Errors
RuntimeErrorThe configuration does not grantfs.writefor this path.
Examples
A file a folder and nothing
<?nvs// Shows what `Core\IO::isWritable` returns for a file, a folder and a path with nothing at it.
string $dir = Core\IO::createTemporaryDirectory();string $file = Core\Path::join($dir, 'notes.txt');Core\IO::write($file, 'hello');
// The program may change this file. This prints "true".echo Core\IO::isWritable($file) ? 'true' : 'false', "\n";
// The program may create files in this folder. This prints "true".echo Core\IO::isWritable($dir) ? 'true' : 'false', "\n";
// Nothing has this name. `isWritable` does not throw an error. This prints "false".echo Core\IO::isWritable(Core\Path::join($dir, 'missing.txt')) ? 'true' : 'false', "\n";truetruefalseCheck the folder before a new file
<?nvs// Shows that you check the folder, not the file, before you create a new file.
string $dir = Core\IO::createTemporaryDirectory();string $report = Core\Path::join($dir, 'report.csv');
// The file does not exist yet, so `isWritable` returns `false`. This prints "file: false".echo 'file: ', Core\IO::isWritable($report) ? 'true' : 'false', "\n";
// The folder is the right path to check. This prints "folder: true".echo 'folder: ', Core\IO::isWritable(Core\Path::parent($report)) ? 'true' : 'false', "\n";
// After `write` creates the file, it is writable. This prints "file: true".Core\IO::write($report, "id,total\n");echo 'file: ', Core\IO::isWritable($report) ? 'true' : 'false', "\n";file: falsefolder: truefile: trueCheck the log folder at start
<?nvs// Shows how a program checks its log folder when it starts, and stops with a clear message if it cannot write there.
class Startup{ public static function check(string $logDir): string { if (!Core\IO::isDirectory($logDir)) { return 'the log folder is missing'; } if (!Core\IO::isWritable($logDir)) { return 'the log folder is not writable'; } return 'ok'; }}
string $logs = Core\Path::join(Core\IO::createTemporaryDirectory(), 'logs');
// The folder does not exist yet. This prints "the log folder is missing".echo Startup::check($logs), "\n";
// After `makeDirectory` creates it, the check passes. This prints "ok".Core\IO::makeDirectory($logs);echo Startup::check($logs), "\n";Core\IO::append(Core\Path::join($logs, 'app.log'), "started\n");the log folder is missingok