Hi, i'm making an Yii2 Basic application and have a file upload form in the admin area. The file upload sends files to app/web/uploads. I followed the great tutorial on uploading files from samdark. It can be seen here: https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md
嗨,我正在制作一个Yii2 Basic应用程序,并在管理区域中有一个文件上传表单。文件上传将文件发送到app / web / uploads。我遵循了从samdark上传文件的精彩教程。可以在这里看到:https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md
What i need to do is to create a view that renders hyperlinks to each one of the files inside uploads folder.
我需要做的是创建一个视图,呈现上传文件夹中每个文件的超链接。
In Yii1.xx there was an extension for handling files called Cfile very handy. I used in several applications to do what i want now.
在Yii1.xx中有一个扩展来处理名为Cfile的文件非常方便。我在几个应用程序中使用我现在想做的事情。
Using Cfile i was able to write code like this:
使用Cfile我能够编写如下代码:
$cfileDir = Yii::app()->file->set('pdfs/'); // set pdfs as target folder
$ cfileDir = Yii :: app() - > file-> set('pdfs /'); //将pdfs设置为目标文件夹
$files = $cfileDir->getContents();
$ files = $ cfileDir-> getContents();
The getContents() method was great because it let me later apply a foreach loop and list all the files in the folder.
getContents()方法很棒,因为它让我以后应用foreach循环并列出文件夹中的所有文件。
In Yii2 how to do something similar in uploads folder, ie, list of files in that folder and create hyperlinks in a view.
在Yii2中如何在uploads文件夹中执行类似的操作,即该文件夹中的文件列表并在视图中创建超链接。
To crate the hyperlinks inside the view i could use Html::a(), but to get all the files inside it i don't know how to do it.
为了创建视图中的超链接,我可以使用Html :: a(),但要获取其中的所有文件,我不知道该怎么做。
Any Ideas ?? Thanks.
有任何想法吗 ??谢谢。
EDIT
编辑
SOLVED with the great tip from ALI.
解决了ALI的精彩提示。
HERE IS THE COMPLETE BLOCK OF CODE
这是完整的代码块
<?php
$files=\yii\helpers\FileHelper::findFiles('uploads/');
if (isset($files[0])) {
foreach ($files as $index => $file) {
$nameFicheiro = substr($file, strrpos($file, '/') + 1);
echo Html::a($nameFicheiro, Url::base().'/uploads/'.$nameFicheiro) . "<br/>" . "<br/>" ; // render do ficheiro no browser
}
} else {
echo "There are no files available for download.";
}
?>
1 个解决方案
#1
27
In Yii2
you can achieve this by using FileHelper Class like below:
在Yii2中,您可以通过使用FileHelper类实现此目的,如下所示:
$files=\yii\helpers\FileHelper::findFiles('/path/to');
Now, you have all files list into $files
variable as an array.
现在,您将所有文件列为$ files变量作为数组。
findFiles()
method, returns the files found under the specified directory and sub-directories.
findFiles()方法,返回在指定目录和子目录下找到的文件。
Another examples:
另一个例子:
\yii\helpers\FileHelper::findFiles('.',['only'=>['*.php','*.txt']]);
Above example lists all files only with php
and txt
extensions.
上面的示例列出了仅使用php和txt扩展名的所有文件。
\yii\helpers\FileHelper::findFiles('.',['except'=>['*.php','*.txt']]);
Above example lists all files with all extensions, except php
and txt
extensions.
上面的示例列出了所有扩展名的文件,除了php和txt扩展名。
\yii\helpers\FileHelper::findFiles('.',['recursive'=>FALSE]);
Above example does not list files under the sub-directories
上面的示例没有列出子目录下的文件
#1
27
In Yii2
you can achieve this by using FileHelper Class like below:
在Yii2中,您可以通过使用FileHelper类实现此目的,如下所示:
$files=\yii\helpers\FileHelper::findFiles('/path/to');
Now, you have all files list into $files
variable as an array.
现在,您将所有文件列为$ files变量作为数组。
findFiles()
method, returns the files found under the specified directory and sub-directories.
findFiles()方法,返回在指定目录和子目录下找到的文件。
Another examples:
另一个例子:
\yii\helpers\FileHelper::findFiles('.',['only'=>['*.php','*.txt']]);
Above example lists all files only with php
and txt
extensions.
上面的示例列出了仅使用php和txt扩展名的所有文件。
\yii\helpers\FileHelper::findFiles('.',['except'=>['*.php','*.txt']]);
Above example lists all files with all extensions, except php
and txt
extensions.
上面的示例列出了所有扩展名的文件,除了php和txt扩展名。
\yii\helpers\FileHelper::findFiles('.',['recursive'=>FALSE]);
Above example does not list files under the sub-directories
上面的示例没有列出子目录下的文件