I
I
Ivan Eremin2019-07-19 10:43:35
1C-Bitrix
Ivan Eremin, 2019-07-19 10:43:35

How to display a list of files with links in the Bitrix directory?

There is a list of files, for example in /upload/
Files are not uploaded to the infoblock. It is necessary to display them in a list with download links.
From the infoblock is done like this:

<?foreach ($arResult["PROPERTIES"]["FILES"]["VALUE"] as $value):?>
  <a href="<?=CFile::GetPath($value)?>">Скачать</a>
<?endforeach?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Eremin, 2019-07-19
@divanus

use \Bitrix\Main\{ Application, IO };

$iterator = new \RecursiveIteratorIterator(
    new \RecursiveDirectoryIterator(Application::getDocumentRoot() . '/upload/MY_FOLDER/', \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
    \RecursiveIteratorIterator::SELF_FIRST
);

foreach($iterator as $item) {
    if ($item->isFile()) {
        $file = new IO\File($item->getPathname());

        echo \sprintf('<a download href="/%s">%s</a>', \str_replace(Application::getDocumentRoot(), '', $file->getPath()), $file->getName());
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question