toFile() not working any more


I want to bypass Kirby processing exceptionally to display a slide. This snippet worked fine in <3:

if ($file = $page->contentfile()->toFile()) {

It seems now that contentfile irritates Kirby 3. But even when I switch to eg. showfile (which gets the correct filename to display) I get http 404, though the file sits in the same folder.

I am curious to learn whether a Kirby 3 change interfere here or if I am simply stuck with some permission issue or alike in the new server context!

Many thanks in advance,

If you don’t want to rename the field, you can use content()->contentfile():

if ($file = $page->content()->contentfile()->toFile()) {
  echo $file;

However, there is no show() method anymore… Depending on what sort of file we are dealing with (sag, for example), you might want to use $file->read().

Hello texnixe

Many thanks for the fast reply :-). Unfortunately this tiny problem still bothers me:

It seems that toFile() fails because a subsequent $file->exists() results in:

Call to a member function exists() on null

My question: Which “working directory” is active when my code resides in a template-file in “/templates”? And how can I make it accessing the file either in `/content’ or ‘/assets’?

The file to be displayed is a simple HTML-File…

Many thanks in advance,

Too many question in one go :wink:

  1. I don’t see how you are using exists() because that’s not in the code snippet above. If used within the if-statement, the error shouldn’t appear. Please post all relevant code.

  2. $page in a template file always refers to the currently active page.

  3. Files in the content folder are accessible via the $site object:

$file = $site->files()->first();

But I don’t think you can upload html files.

Hello texnixe and all

I was able to solve it!

Your hint to use echo $file; instead of former $file->show(); was perfectly right, of course – I never doubted it ;-).

However, on Kirby 3 I always got a http 404, when pointing the browser eg. to, which worked perfectly fine <3.

Now I found out that I have to add the content folder:

I feel relieved that I can proceed now with my upgrade.

Anyhow, in case you have an explanation about this slight difference I look forward to learn more: It seems that Kirby <3 absorbed the content folder despite the bypassing, and that Kirby 3 requires it, when being bypassed.

Kind regards,

Dear texnixe

It seems that this issue does not let me go. I want to directly read in and display the file test.html, bypassing all other kirby functionality. Actually the “toFile()” does not work, for unclear reasons.

Here the complete template with the issue and responses commented inside:

// I want to directly read in and display the file test.html, bypassing all other kirby functionality
// meta.txt is sitting in the folder 
// with the field inside: 
// contentfile: test.html 
// test.html also resides in the same content folder 

$file = $page->content()->contentfile();
echo $file; // > test.html
echo gettype($file); // > object
echo $file->exists(); // > 1
if ($filecontent = $file->toFile()) {
  echo $filecontent->read(); // is never entered
  } else {
  echo "failed"; // > failed is displayed each time

Many thanks for providing insights & taking care,

I think the main problem here is that an html file is not a file in the Kirby sense. As I said above, it is not even possible to upload html files via the Panel. Yes, you can place them into a folder, and you can probably read them with plain PHP, but since these files are not files, the toFile() method fails.

Hello texnixe

Many thanks for this clarification. In this case I do not need panel support. Interestingly that toFile does not work on HTML-files. I managed to get it work, finally:

 $file = $page->content()->contentfile();
 $filename = $page->root()."/".$file;
 if ($filecontent = readfile ($filename)) {
    echo $filecontent; 

Also refers to this earlier issue: Display content from HTML file on a page?