when the download method is called it’s starting a download with the correct file/filename, but the file downloaded has 0 bytes and is obviously empty.
i am pretty much using the function vanilla inside a function declaration after checking a few things…
// $file object as seen above
$file->download();
exit;
the function is declared in a seperate file which will be loaded by plugin/my-plugin/index.php
function fileDownload($var1,$var2){
// ...
$file->download();
exit;
}
the function itself is called in a route
<?php return [
'pattern' => '(:any)/download/(:any)',
'action' => function($language,$file){
// some stuff happening....
// $page & $file var is set correctly, as mentioned the $file object within the function returns the correct file which should download
fileDownload($page, $file);
}
]; ?>
that’s not the problem here, before posting the code i edited out some unnecessary stuff and put in that error here.
my code will run though some pages and structures to get the $file object ready, i was trying to strip down a bit before posting.
as i mentioned. everything seems to get set up correctly, the $file object is returning everything from size and filename, but when the actual $file->download() is happening, it’ll return 0 bytes.
i replaced $file->download() with the following, and this seems to work fine.
// from here https://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file->url()) . "\"");
readfile($file->url()); // do the double-download-dance (dirty but worky)
you mean in the kirby core, there is no download() method? well ok, i have checked the docs and was wondering why i couldn’t find it anymore, there has been a download method before, and somehow some things still happen, …
There are three download methods in the Kirby core, but no $file->download() (as in Kirby\Cms\File) method. Maybe it inherits from the image class, though, haven’t checked.
well ok, so maybe i expected the unexpected. in v2 there was a $file->download() method, and before i checked the docs some things were still happening when using the download() method (with a $file object) which led me to believe it’s still there
after checking the docs it seemed like the method is not documented, so i was taking a shot here. If that method is gone (or never was there to begin with) … that’s fine.
There is in fact a download method in the Kirby\Image\Image class and $file->download does in fact download something, but when I do that, I only get a file with an html extension …
if ($file = $page->mypdf()->toFile()) {
$file->download();
}
// or shorter
($file = $page->mypdf()->toFile()) ? $file->download() : false;
Your code would actually work as well (but is more verbose), but you should never call a class method without making sure to have an object of that class.
Thanks Sonja for the quick response, im still learning the basics and dos and don’ts of Kirby.
Your code works But!.. It downloads not the expected file - just putting the source of the the current page in a file and download it. Mehh. But the workaround from carsten did it.