I am using the PHP library sabre/dav and try to create a very basic list of folders and files based on a Nextcloud I own. The following loop is successfully looping through my files, but I can’t understand how to create links to them so I can download or open it.
Edit: the following code will list my files and create links like: .php/dav/files/SHARED-MEDIA/sound.mp3
, if I click on those, a JSON viewer shows in the browser, with the message error: "Strict Cookie has not been found in request"
.
I have no idea what the download URL should look like.
Any help would be very welcome!
$settings = array(
'baseUri' => '.php/dav',
'userName' => '*********',
'password' => ''*********'
);
$client = new Sabre\DAV\Client($settings);
$directory = 'files/SHARED-MEDIA';
try {
$result = $client->propFind($directory, [
'{DAV:}displayname',
'{DAV:}getlastmodified',
], 1);
echo "<ul>"; // Start an unordered list
foreach ($result as $file) {
$name = $file['{DAV:}displayname'];
$url = $settings['baseUri'] . "/" . $directory . "/" . urlencode($name);
echo "<li>$name <a href='$url'>Download</a>";
if (str_contains($name, '.mp3')) {
echo " <audio controls><source src='$url' type='audio/mpeg'></audio>";
}
echo "</li>";
}
echo "</ul>";
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}