I need to protect a .csv that is stored in the content folder. I’m using the ‘Protecting Files’ cookbook to stop access, however I still need to access the file via code for using the CSV.
<?php
Kirby::plugin('cookbook/files-firewall', [
'components' => [
'file::url' => function ($kirby, $file) {
if ($file->template() === 'protected') {
return $kirby->url() . '/' . $file->parent()->id() . '/' . $file->filename();
}
return $file->mediaUrl();
},
],
'routes' => [
[
'pattern' => 'register/(:any)',
'action' => function() {
return site()->errorPage();
},
],
],
]);
I guess I need to add an if statement to the route action, but I have no idea how to write that. Everything I’ve tried still returns an error. Any ideas? Or should I move the file out of the content folder?
The file url component should not prevent access to the CSV file via the file system, but only make the public url inaccessible. What are you trying to do in your script?
I’m using your csv helper to get the file:
$csv = csv(page('register')->file('subscriptions.csv'), ';');
<?php
function csv(string $file, string $delimiter = ','): array
{
$lines = file($file);
$lines[0] = str_replace("\xEF\xBB\xBF", '', $lines[0]);
$csv = array_map(function($d) use($delimiter) {
return str_getcsv($d, $delimiter);
}, $lines);
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv);
return $csv;
}
As soon as I add the files-firewall plugin the helper is returning an error:
file(http://localhost:8888/********/register/subscriptions.csv): Failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
If I remove the files-firewall, it loads no problem.
Because this needs a path, not a file object, so page('register')->file('subscriptions.csv')->root()
.
1 Like
Ah, of course! Thank you!