PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] How to find file without specific file extension in laravel storage?

 March 18, 2022     laravel, laravel-5, php, storage     No comments   

Issue

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.


Solution

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);
}


Answered By - jedrzej.kurylo
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing