List file and directory in php

PHP: список файлов и директорий

В данном уроке мы разберемся с типовой задачей, которая возникает во время работы над проектом PHP: получение списка файлов и директорий. Обсудим несколько базовых и более изощренных подходов, с перечисление плюсов и минусов каждого. Первые три решения будут использовать типовые функции PHP, а затем представим более надежное с применением итераторов SPL.

Для предметного обсуждения решения и демонстраций предположим, что структура директорий имеет следующий вид:

\---manager | \---user | \---document.txt | \---data.dat | \---style.css |---article.txt |---master.dat |---script.php |---test.dat |---text.txt

Базовые решения

Первый набор решений основан на использовании функции glob() , комбинации функций opendir() , readdir() и closedir() , и также функции scandir() .

Использование glob()

Первое решение базируется на использовании функции glob(), которая позволяет выполнять поиск пути с помощью шаблонов. Функция имеет два параметра:

  • $pattern (обязательный): шаблон поиска
  • $flags (опциональный): один или несколько флагов, описание которых можно найти в документации
Читайте также:  Сервера css source сервер

Рассмотрим примеры. Для поиска в директории всех файлов и директорий, имена которых заканчиваются на .txt, следует использовать код:

Если вывести переменную $filelist , то получим:

array ( 0 => 'article.txt', 1 => 'text.txt' )

Если нужен список файлов и директорий, имена которых начинаются на “te”, то код будет выглядеть так:

array ( 0 => 'test.dat', 1 => 'text.txt' )

А для получения списка только директорий с именами, содержащих “ma”, используем код:

Обратите внимание, что в последнем примере использован флаг GLOB_ONLYDIR в качестве второго параметра функции. Поэтому файл master.dat исключен из списка. Хотя функция glob() очень проста в использовании, иногда она недостаточно гибкая. Например, нет флага для получения только файлов (без директорий), которые соответствуют шаблону.

Используем opendir() , readdir() , и closedir() .

Второй подход к получению списка файлов и директорий, который мы обсудим, заключается в использовании функций opendir() , readdir() , и closedir() .

Функция opendir() открывает директорию и возвращает дескриптор соединения. Как только дескриптор получен, можно использовать функцию readdir() . С каждым обращением данная функция выдает имя следующего файла или директории внутри открытого каталога. Если все имена уже были перечислены, функция возвращает false. Для закрытия дескриптора используется функция closedir() .

В отличие от использования функции glob() , данный подход сложнее, так как у вас нет параметров, которые помогают фильтровать список возвращаемых имен файлов и директорий. Вы должны выполнить фильтрацию самостоятельно, чтобы получить нужный результат.

Ниже приведенный пример возвращает список имен файлов и директорий начинающихся на “te”:

При выполнении выше приведенного кода, переменная $entry будет содержать такие включения, как “.” и “..”. Это две виртуальные директории, которые имеются в каждом каталоге файловой системы. Они представляют текущий каталог и родительский каталог соответственно.

Второй пример выводит только файлы, содержащиеся в заданном каталоге.

array ( 0 => 'article.txt', 1 => 'master.dat', 2 => 'script.php', 3 => 'test.dat', 4 => 'text.txt' )

Использование scandir()

В завершение представим функцию scandir() . Она имеет только один обязательный параметр: путь для чтения. Функция возвращает массив файлов и директорий, расположенных по указанному пути. Для получения списка файлов и директорий по определенному критерию нужно выполнить дополнительную фильтрацию. С другой стороны, решение получается более кратким и не требует управления дескрипторами.

Данный пример показывает, как получить список файлов и каталогов, имена которых начинаются на “te”:

Воспользуемся итераторами SPL

Теперь рассмотрим использование итераторов SPL. Но прежде, чем приступить к решению нашей задачи, проведем введение в библиотеку SPL и итераторы. Библиотека SPL предоставляет серию классов для объектно ориентированных структур данных, итераторов, дескрипторов файлов и прочее.

Одно из преимуществ итераторов заключается в том, что они являются классами и их можно расширить для удовлетворения собственных нужд. Другой плюс состоит в том, что итераторы имеют собственные методы, которые являются полезными при решении множества типовых задач и располагаются в одном месте. Посмотрите на пример использования FilesystemIterator в сравнении с readdir() . Оба метода применяют цикл, но в случае readdir() вы обрабатываете только строку, а FilesystemIterator работает с объектом, который может содержать дополнительную информацию о файле или директории (размер, владелец, права доступа и так далее).

Конечно, PHP представляет возможность для получения такой информации с помощью функций,например filesize() и fileowner(). Но PHP5 основан на использовании концепции ООП. Поэтому лучше использовать современные методы работы с языком программирования. На нашем сайте есть уроки, посвященные работе с итераторами.

Как уже сообщалось во водной части урока, мы рассмотрим использование FilesystemIterator , RecursiveDirectoryIterator и GlobIterator . Первый наследуется от DirectoryIterator , а остальные от FilesystemIterator . Они все имеют один и тот же конструктор, который принимает два параметра:

  • $path (обязательный): путь к пункту файловой системы, над которым совершаются операции
  • $flags (опциональный): один или несколько флагов, перечисленных в документации

Реальное различие в данных итераторах заключается в их использовании для навигации по заданному пути.

FilesystemIterator

Использовать FilesystemIterator очень просто. Рассмотрим в действии. Представляем два примера. Первый показывает поиск всех файлов и каталогов, имена которых начинаются на “te”. Второй пример использует другой итератор RegexIterator для поиска всех файлов и каталогов, имена которых заканчиваются на “t.dat” или “t.php”. Итератор RegexIterator используется для фильтрации результата на основе регулярных выражений.

getFilename(), "te") === 0) < $filelist[] = $entry->getFilename(); > >

Выше приведенный код выдаст результат, аналогичный предыдущим примерам.

Второй пример с применением RegexIterator :

Источник

How to List Files in a Directory in PHP

Sajal Soni

Sajal Soni Last updated Aug 31, 2021

In this article, we’ll discuss how you can get a list of all the files in a directory in PHP.

In your day-to-day PHP development, you often need to deal with a file system—for example, getting a list of files in a specific directory. PHP provides a few different ways of reading the contents of a directory easily. Today, we’ll go through all these methods, along with examples to understand how each one works.

The opendir , readdir , and closedir Functions

In this section, we’ll discuss the opendir , readdir , and closedir functions to see how you can use them to get a list of files in a specific directory.

The opendir function allows you to open up a directory handle, which you can use along with other functions for different operations on the directory. You need to pass the path to the directory in the first argument of the opendir function. If the directory path is valid, a directory handle resource will be returned.

The readdir function allows you to read a directory. You need to provide a valid directory handle in the first argument of the readdir function, and you can iterate over all the entries and get a list of all files in a directory.

The closedir function allows you to close the directory handle which is opened by the opendir function. It’s a good practice to use the closedir function once you’re done with your operations on the directory handle (which was initially opened by the opendir function).

Now, let’s see it in action in the following example.

Источник

4 Ways To List Files & Folders In PHP (Simple Examples)

Welcome to a tutorial on how to list files and folders in PHP. So you need to get the contents of a folder in PHP? Well, be prepared for a small nasty surprise.

  • foreach (scandir(«FOLDER») as $ff) < . >
  • foreach (glob(«FOLDER/*») as $ff) < . >
  • Open and read the folder.
    • $dh = opendir(«FOLDER»);
    • while ($ff = readdir($dh))
    • Use an iterator.
      • $it = new DirectoryIterator(«FOLDER»);
      • foreach ($it as $ff)

      That covers the basics, but just what is the difference? Read on for more examples!

      TLDR – QUICK SLIDES

      How To List Files & Folders In PHP

      TABLE OF CONTENTS

      PHP GET FILES FOLDERS

      All right, let us now get into the examples of getting files and folders in PHP.

      METHOD 1) SCANDIR

      1A) BASIC SCANDIR

       - file "; > if (is_dir($dir . $ff)) < echo "- folder "; > >
      • scandir(«FOLDER») will give you the full list of files and folders within the specified folder in an array.
      • Take note, this also includes the hidden . and .. . For those who are new, these represent the current and parent folder respectively.
      • We are not interested in the “dots”, so we remove them using array_diff() .
      • The rest is self-explanatory – Loop through the files and folders.

      1B) SCANDIR READ SUB-FOLDERS

       - file "; > if (is_dir($dir . $ff)) < echo "- folder "; rscan("$dir$ff/"); > > > // (B) GO! rscan("YOUR/FOLDER/");

      If you need to “dig” into the sub-folders, create a recursive function – If the entry is a folder, pass it back into the recursive function itself.

      METHOD 2) GLOB

      2A) BASIC GLOB

      // (A) GET FILES/FOLDERS $all = glob("YOUR/FOLDER/*"); // $all = glob("YOUR/FOLDER/*.", GLOB_BRACE); $eol = PHP_EOL; // (B) LOOP THROUGH ALL foreach ($all as $ff) < if (is_file($ff)) < echo "- file "; > if (is_dir($ff)) < echo "- folder "; > >

      Now, scandir() will fetch everything. If you only need certain file types – glob() is the best option.

      2B) GLOB READ SUB-FOLDERS

      // (A) RECURSIVE GLOB function rglob ($dir, $ext="*") < // (A1) GET FILES $eol = PHP_EOL; foreach (glob("$dir$ext", GLOB_BRACE) as $ff) < if (is_file($ff)) < echo "- file "; > > // (A2) GET FOLDERS foreach (glob("$dir*", GLOB_ONLYDIR | GLOB_MARK) as $ff) < echo "- folder "; rglob($ff, $ext); > > // (B) GO! rglob("YOUR/FOLDER/"); // rglob("YOUR/FOLDER/", "*.");

      A “recursive glob” is slightly more roundabout, but basically – Get the files first, then recursive “dig” into the folder.

      METHOD 3) OPENDIR

      3A) BASIC OPENDIR

       - file "; > if (is_dir($ff)) < echo "- folder "; > >>

      The above methods will “fetch everything into an array”. Some of you sharp code ninjas should have already caught the potential problem – What if there are thousands of files in the folder? This will run into performance and memory issues. So instead of “getting everything at once”, opendir() and readdir() will read “entry by entry”.

      3B) OPENDIR READ SUB-FOLDERS

       - file "; > if (is_dir($ff)) < echo "- folder "; rread("$ff/"); > >> > // (B) GO! rread("YOUR/FOLDER/");

      Well, this should not be a surprise by now. Create a recursive function to open and read sub-folders.

      METHOD 4) DIRECTORY ITERATOR

      4A) BASIC ITERATOR

      isDot()) < continue; >if ($ff->isFile()) < echo "getFilename()> - file "; > if ($ff->isDir()) < echo "getFilename()> - folder "; > >

      Lastly, here is the “alternative” way to read a folder entry by entry – Using a DirectoryIterator .

      4B) ITERATOR READ SUB-FOLDERS

      isDot()) < continue; >if ($ff->isFile()) < echo "getFilename()> - file "; > if ($ff->isDir()) < echo "getFilename()> - folder "; riterate("getFilename()>/"); > > > // (B) GO! riterate("YOUR/FOLDER/");

      Well… You guys should be masters of recursive functions by now.

      DOWNLOAD & NOTES

      Here is the download link to the example code, so you don’t have to copy-paste everything.

      SUPPORT

      600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a «paid scripts and courses» business, so every little bit of support helps.

      EXAMPLE CODE DOWNLOAD

      Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

      That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

      EXTRA) COMBINING GLOB & ITERATOR

      // (A) GLOB ITERATOR $dir = "D:/DOCS/"; $eol = PHP_EOL; // $iterator = new ArrayObject(glob("$dir*.", GLOB_BRACE)); $iterator = new ArrayObject(glob("$dir*", GLOB_BRACE)); $iterator = $iterator->getIterator(); /* (B) OPTIONAL - PAGINATION $pgNow = 0; // current page $pgPage = 10; // entries per page $iterator = new LimitIterator($iterator, $pgNow * $pgPer, $pgPer); */ // (C) LOOP foreach ($iterator as $ff) < if (is_file($ff)) < echo "- file "; > if (is_dir($ff)) < echo "- folder "; > >

      Which is the “best method”? I will say “all of them”, whichever works for you is the best. But personally, my “best solution” is a combination of glob and iterator – This is very flexible, capable of restricting by the file types, and also easily do pagination with it.

      TUTORIAL VIDEO

      INFOGRAPHIC CHEAT SHEET

      THE END

      Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

      Источник

Оцените статью