Can't create file from PHP script


Im trying to create a file on the server using the f::write() method.

The script is located in assets/functions/zip-multiple.php. It is fetched from the front-end. It throws an error 500.

assets/functions/zip-multiple.php only contains a script suggested by @texnixe few years ago on the forum :

$path = 'http://localhost:8888/tmnlab-exp_2/assets/files/test.txt';
try {
    $file = F::write($path, 'test');
} catch(Exception $e) {
    throw new Exception('The file could not be created');

Any idea ?

That’s a url not a path, you need a local file path here.

Thank you for your answer @pixelijn.

You mean something like ../files/test.txt ?
it doesn’t work neither.

This code works :
assets/functions/zip-multiple.php :

$path = '../files/test.txt';
$file = fopen($path, "wb");
fwrite($file, 'test');

It seems that the f::write() isn’t accessible from the assets folder.

If you want to access files in your Kirby project, you can always use the roots, e.g.

kirby()->root('index') . '/somefileintherootfolder';
kirby()->root('assets') . '/path/to/file/in/assets/folder/file.txt';
// etc.

1 Like