最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Webdav: Listing files and folder - Stack Overflow

programmeradmin4浏览0评论

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();
}
发布评论

评论列表(0)

  1. 暂无评论