V
V
Vyacheslav2015-10-26 15:48:49
PHP
Vyacheslav, 2015-10-26 15:48:49

How to find all files in a folder by pattern?

Pictures are stored in the folder template - [name-number.jpg]
number - a number from 0 to unknown It is
necessary in php to display all the pictures on the screen for a specific name
To be honest, I don’t even know where to start, I haven’t dealt with regular expressions yet.
As I understand it, you need to use a regular expression to search for images in a folder and display the search result in a php loop.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2015-10-26
@noxiconum

First scan the directory: scandir
Then iterate over the resulting array in foreach .
You can do it without regular expressions if you don't know them:

$fileName = 'name-23.jpg';
$fragments = explode( '-' , $fileName, 2 );
// в результате в $fragments[0] будет попадать `name`

A
Aleksey Ratnikov, 2015-10-26
@mahoho

Use shell tools quickly and conveniently:

$output = shell_exec("find /path/to/dir -iname filename* -exec basename {} \;");
$fileNames = explode("\n", $output);

array_pop($fileNames); //последний элемент будет пуская строка

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question