How to create a new file on server?

Hello,

I like to create a new file if it does not exist and save some data of the blog in it.

Is there a way with kirby code (possibly createNewFilename(), which I did not manage to work)? Or is the only way by using PHP fopen?

The relevant code snippet so far:

$tagfile = $pages->find(c::get('blog', 'blog'))->files()->find('tags_do_not_delete.json');
if(!isset($tagfile)):
   $tagfile = ...
endif;
$blogdata = $tagfile->read('json');

I am really new with kirby and PHP but willing to learn a lot. So please be patient with me.

Lule

You can use f::write, for example, wrapped in a function:

function createFile($path) {
  try {
    $file = f::write($path, '');
  } catch(Exception $e) {
    throw new Exception('The file could not be created');
  }
}

Thanks alot. This is the function I am using:

function createFileAndSave($path, $data) {
	$content = '';
	if(isset($data)):
		$content = data::encode($data, 'json');
	endif;
	try {
		$file = f::write($path, $content, false);
	} catch(Exception $e) {
		throw new Exception('The file could not be created');
	};
};