Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find file without specific file extension in laravel storage?

How to find file by name without specific extension in laravel Storage?

like this "filename.*"

Storage::get("filename.*")

I tried this but seems not to work. It searches for specific file with specific extension.

like image 411
John Roca Avatar asked Aug 22 '16 05:08

John Roca


People also ask

How do I get the path of a file in Laravel storage?

Retrieve the file pathphp $storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();

What is __ DIR __ In Laravel?

__DIR__ is the current directory you are in, if you wanted to go back one step, you could use dirname. This is used such as; dirname(__DIR__);

How do I get files in Laravel?

If you have file object from request then you can simply get by laravel function. $extension = $request->file->extension(); dd($extension); If you have file object from request then you can simply get by laravel function.

How do I delete files from storage in Laravel?

One way to delete a file from the public directory in Laravel is to use the Storage facade. To delete a file, you will need to follow the following steps: Step 1: Check to ensure that the folder and file exist. Step 2: Delete the required file.


2 Answers

Storage::get() takes a file path as a parameter and returns the content of a single file identified by this path or throws FileNotFoundException if file can't be found.

Wildcards are not supported in the path - one reason for that could be that there might be multiple files that match the path with wildcards which would break the rule that content of a single file is returned from Storage::get(). Scanning the whole folder would also be much slower, especially with remote storages.

However, you could get what you want using other functionality that Storage facade offers. First, list the content of your storage - that will give you the list of all available files. Then filter the list yourself to get the list of matching files.

// list all filenames in given path
$allFiles = Storage::files('');

// filter the ones that match the filename.* 
$matchingFiles = preg_grep('/^filename\./', $allFiles);

// iterate through files and echo their content
foreach ($matchingFiles as $path) {
  echo Storage::get($path);
}
like image 185
jedrzej.kurylo Avatar answered Nov 01 '22 09:11

jedrzej.kurylo


Accepted solution works. However I've found this other and I like it more:

$matchingFiles = \Illuminate\Support\Facades\File::glob("{$path}/*.log");

See reference here: http://laravel-recipes.com/recipes/143/finding-files-matching-a-pattern

like image 20
mayid Avatar answered Nov 01 '22 09:11

mayid