Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a list of files from a folder using PHP? [closed]

Tags:

file

php

I want to read a list the names of files in a folder in a web page using php. is there any simple script to acheive it?

like image 324
Arun Annamalai Avatar asked Apr 06 '09 09:04

Arun Annamalai


People also ask

How can I see all PHP files in a directory?

The scandir() function returns an array of files and directories of the specified directory.

How can I get a list of all the subfolders and files present in a directory using PHP?

PHP using scandir() to find folders in a directory The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.


1 Answers

The simplest and most fun way (imo) is glob

foreach (glob("*.*") as $filename) {     echo $filename."<br />"; } 

But the standard way is to use the directory functions.

if (is_dir($dir)) {     if ($dh = opendir($dir)) {         while (($file = readdir($dh)) !== false) {             echo "filename: .".$file."<br />";         }         closedir($dh);     } } 

There are also the SPL DirectoryIterator methods. If you are interested

like image 74
Ólafur Waage Avatar answered Oct 09 '22 02:10

Ólafur Waage