3

Currently trying to play about with files but struggling to figure out where to put them and how to read them back in a list.

Ive tried putting few test files into

$files = array();
$dir = opendir(asset('files'); // open the cwd..also do an err check.
while(false != ($file = readdir($dir))) {
    if(($file != ".") and ($file != "..") and ($file != "index.php")) {
            $files[] = $file; // put in array.
    }   
}

but it just returns blank despite having 3 test files in it.

Looked into larval recipes and suggestions say File:allFile() which isn't a supported method but its got me wondering apart from how to read files from a directory, where should i be really storing files I'm going to have on a server.

1 Answer 1

4

Laravel 5.3 uses Storage instead of Files. You can access all of the files in a directory using either of these two methods:

use Illuminate\Support\Facades\Storage;

$files = Storage::files($directory);

$files = Storage::allFiles($directory); // Includes subdirectories
Sign up to request clarification or add additional context in comments.

8 Comments

Passing it a public director path it seems to return empty array?
Make sure you're passing in the correct path. For the Storage class, I believe that it should be relative to your project directory. So if your folder is storage/files, that's what you should pass in. You may need to tweak it. You can also try logging Storage::dirname($directory); to find out exactly where it's looking.
Files I've put to test are in storage/files just simple test.txt however when i set $directory = storage/files its still returning email
Try the logging and make sure it's looking where you think it is. Sometimes file paths get a little wonky. Also check the permissions of the files.
Using it on my local machine so set the permission to 777, set the directory to 1. $directory = "storage/files/"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.