18

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

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.

Using Cfile i was able to write code like this:

$cfileDir = Yii::app()->file->set('pdfs/'); // set pdfs as target folder

$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.

In Yii2 how to do something similar in uploads folder, ie, list of files in that folder and create hyperlinks in a view.

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.

Any Ideas ?? Thanks.

EDIT

SOLVED with the great tip from 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.";
    }
?>
André Castro
  • 1,527
  • 6
  • 33
  • 60

1 Answers1

34

In Yii2 you can achieve this by using FileHelper Class like below:

$files=\yii\helpers\FileHelper::findFiles('/path/to');

Now, you have all files list into $files variable as an array.

findFiles() method, returns the files found under the specified directory and sub-directories.

Another examples:

\yii\helpers\FileHelper::findFiles('.',['only'=>['*.php','*.txt']]);

Above example lists all files only with php and txt extensions.

\yii\helpers\FileHelper::findFiles('.',['except'=>['*.php','*.txt']]);    

Above example lists all files with all extensions, except php and txt extensions.

\yii\helpers\FileHelper::findFiles('.',['recursive'=>FALSE]);

Above example does not list files under the sub-directories

Ali MasudianPour
  • 14,329
  • 3
  • 60
  • 62
  • Many thanks Ali for the rapid response. Nevertheless i couldn't implement it because PHP always throws the error: Array to string conversion. In PHP we could use print_r to print an array element by element, but in Yii2 i don't know what to do. Ideas? – André Castro Dec 09 '14 at 10:21
  • Ok. I've just solved it using the great tip from Ali, that pointed me in the right way. Used findfiles() method and then iterated it the way i'm gonna show in the EDIT to my question. I will mark Ali's answer as correct. See the EDIT for complete solution. Many thanks for the support. – André Castro Dec 09 '14 at 10:33
  • _In PHP we could use print_r_ You still can : add your array to the view variables and _print_r()_ in the view. **Controller**: `return $this->render('index', ['debug' => $files]);` **View**: `

    `
    – Christian Lescuyer Jul 27 '15 at 13:29
  • C:\wamp\www\realestate/frontend/views/property/\index.php i'm getting full path.is there any option to get last "index.php" alone ? – Vinod C Jan 20 '17 at 11:19
  • 1
    What if I want to search some specific images with there name. Let say there are 1000 images in the folder and I just have to search 4 out of them? – Moeez Feb 07 '18 at 03:16