php get all files in folder

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

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

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?

9 Answers 9

The simplest and most fun way (imo) is glob

But the standard way is to use the directory functions.

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

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

There is this function scandir():

This is what I like to do:

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

If you have problems with accessing to the path, maybe you need to put this:

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

There is a glob. In this webpage there are good article how to list files in very simple way:

Check in many folders :

Folder_1 and folder_2 are name of folders, from which we have to select files.

$format is required format.

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

You can use standard directory functions

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

There is also a really simple way to do this with the help of the RecursiveTreeIterator class, answered here: https://stackoverflow.com/a/37548504/2032235

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

Not the answer you’re looking for? Browse other questions tagged php file or ask your own question.

Linked

Related

Hot Network Questions

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

List all the files and folders in a Directory with PHP recursive function

I’m trying to go through all of the files in a directory, and if there is a directory, go through all of its files and so on until there are no more directories to go to. Each and every processed item will be added to a results array in the function below. It is not working though I’m not sure what I can do/what I did wrong, but the browser runs insanely slow when this code below is processed, any help is appreciated, thanks!

19 Answers 19

Your code :

Output (example) :

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

This will bring you all the files with paths.

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

It’s shorter version :

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

Your code :

Output (example) :

James Cameron’s proposition.

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

This could help if you wish to get directory contents as an array, ignoring hidden files and directories.

The result would be something like;

My proposal without ugly «foreach» control structures is

You may only want to extract the filepath, which you can do so by:

Still 4 lines of code, but more straight forward than using a loop or something.

Here is what I came up with and this is with not much lines of code

It outputs something like

** The dots are the dots of unoordered list.

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

Here’s a modified version of Hors answer, works slightly better for my case, as it strips out the base directory that is passed as it goes, and has a recursive switch that can be set to false which is also handy. Plus to make the output more readable, I’ve separated the file and subdirectory files, so the files are added first then the subdirectory files (see result for what I mean.)

Enjoy! Okay, back to the program I’m actually using this in.

UPDATE Added extra argument for including directories in the file list or not (remembering other arguments will need to be passed to use this.) eg.

$results = get_filelist_as_array($dir, true, », true);

Источник

scandir

scandir — List files and directories inside the specified path

Description

Parameters

The directory that will be scanned.

For a description of the context parameter, refer to the streams section of the manual.

Return Values

Returns an array of filenames on success, or false on failure. If directory is not a directory, then boolean false is returned, and an error of level E_WARNING is generated.

Examples

Example #1 A simple scandir() example

The above example will output something similar to:

Notes

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

See Also

User Contributed Notes 36 notes

Easy way to get rid of the dots that scandir() picks up in Linux environments:

Here is my 2 cents. I wanted to create an array of my directory structure recursively. I wanted to easely access data in a certain directory using foreach. I came up with the following:

How i solved problem with ‘.’ and ‘..’

I needed to find a way to get the full path of all files in the directory and all subdirectories of a directory.
Here’s my solution: Recursive functions!

Needed something that could return the contents of single or multiple directories, recursively or non-recursively,
for all files or specified file extensions that would be
accessible easily from any scope or script.

scandir() with regexp matching on file name and sorting options based on stat().

name file name
dev device number
ino inode number
mode inode protection mode
nlink number of links
uid userid of owner
gid groupid of owner
rdev device type, if inode device *
size size in bytes
atime time of last access (Unix timestamp)
mtime time of last modification (Unix timestamp)
ctime time of last inode change (Unix timestamp)
blksize blocksize of filesystem IO *
blocks number of blocks allocated

Scandir on steroids:
For when you want to filter your file list, or only want to list so many levels of subdirectories.

Источник

Listing all the folders subfolders and files in a directory using php

Please give me a solution for listing all the folders,subfolders,files in a directory using php. My folder structure is like this:

I want to get the list of all the files inside each folder.

Is there any shell script command in php?

18 Answers 18

This code lists all directories and files in sorted order in a tree view. It’s a site map generator with hyper-links to all the site resources. The full web page source is here. You’ll need to change the path on the ninth line from the end.

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

A very simple way to show folder structure makes use of RecursiveTreeIterator class (PHP 5 >= 5.3.0, PHP 7) and generates an ASCII graphic tree.

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

In case you want to use directoryIterator

Following function is a re-implementation of @Shef answer with directoryIterator

I’m really fond of SPL Library, they offer iterator’s, including RecursiveDirectoryIterator.

It will use to make menu bar in directory format

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

If you’re looking for a recursive directory listing solutions and arrange them in a multidimensional array. Use below code:

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

The precedents answers didn’t meet my needs.

If you want all files and dirs in one flat array, you can use this function (found here) :

returns me an array like this :

You can also use dynamic path generation :

With this function :

If you wan get only directories you can use :

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

I was looking for a similar function to this. I needed directories as keys and subdirectories as arrays and files to just be placed as values.
I used the following code:

There’s a lot to take in.

The first function get_files_in_dir function was created so that I could get all files and folders in a directory based on a regular expression. I use it here because it has some error checking to make sure a directory is converted into an array.

Next, we have a simple function that just adds a forward slash to the end of a string if there isn’t one there already.

Finally, we have the get_directory_tree function which will iterate through all the folders and sub folders and create an associative array where the folder names are the keys and files are the values (unless the folder has sub folders).

Источник

PHP list of specific files in a directory

The following code will list all the file in a directory

While this is very simple code it does the job.

10 Answers 10

You’ll be wanting to use glob()

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

A simple way to look at the extension using substr and strrpos

You can also use the recursive variants of the iterators to traverse an entire directory hierarchy.

LIST FILES and FOLDERS in a directory (Full Code):
p.s. you have to uncomment the 5th line if you want only for specific extensions

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

you can mix between glob() function & pathinfo() function like below.

the below code will show files information for specific extension «pdf»

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

You should use glob.

More about using glob and advanced filtering:

But I’d recommend using glob instead too.

php get all files in folder. Смотреть фото php get all files in folder. Смотреть картинку php get all files in folder. Картинка про php get all files in folder. Фото php get all files in folder

You can extend the RecursiveFilterIterator class like this:

Now you can instantiate RecursiveDirectoryIterator with path as an argument like this:

This will list files under the current folder only.

Now run the foreach loop on this iterator. You will get the files with specified extension

Note:— Also make sure to run the ExtensionFilter before RecursiveIteratorIterator, otherwise you will get all the files

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *