php list files in dir

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.

Источник

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 list files in dir. Смотреть фото php list files in dir. Смотреть картинку php list files in dir. Картинка про php list files in dir. Фото php list files in dir

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

There is this function scandir():

This is what I like to do:

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

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

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

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

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 list files in dir. Смотреть фото php list files in dir. Смотреть картинку php list files in dir. Картинка про php list files in dir. Фото php list files in dir

You can use standard directory functions

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

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 list files in dir. Смотреть фото php list files in dir. Смотреть картинку php list files in dir. Картинка про php list files in dir. Фото php list files in dir

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 list files in dir. Смотреть фото php list files in dir. Смотреть картинку php list files in dir. Картинка про php list files in dir. Фото php list files in dir

This will bring you all the files with paths.

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

It’s shorter version :

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

Your code :

Output (example) :

James Cameron’s proposition.

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

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

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 list files in dir. Смотреть фото php list files in dir. Смотреть картинку php list files in dir. Картинка про php list files in dir. Фото php list files in dir

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

Источник

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 list files in dir. Смотреть фото php list files in dir. Смотреть картинку php list files in dir. Картинка про php list files in dir. Фото php list files in dir

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 list files in dir. Смотреть фото php list files in dir. Смотреть картинку php list files in dir. Картинка про php list files in dir. Фото php list files in dir

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

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

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

You should use glob.

More about using glob and advanced filtering:

But I’d recommend using glob instead too.

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

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

Источник

I’m trying to list the content of a specific directory as links and want the browser to open or download the files if I click on them, as it normaly would do like a default html file-link, but the links are not working, if I click. If I right-click and copy the link and paste to a new tab, it opens the file.

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

3 Answers 3

Please have a look suggested code for auto download file when click on generated link.

You ought to have a central processing script for all the files you want to download. So your href will point to the script instead of file itself like this:

And in this script, you need to send content-disposition header for the files to get download, instead of being displayed inline, like this:

Thanks for the replys. I tried them, but did not work for me. I think I miss the basics for this. Finaly I figured out something else:

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

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

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

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.

Источник

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

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