If you're still using the opendir() function to list folders in PHP, then read on as much as possible, and it's faster to do it with the glob() function. Glob is much smarter, as you can see by looking at examples of using glob() in php.
This is a function that finds file paths according to a pattern. Actually, the ability to use path patterns greatly simplifies the work with listing folders and files.
Introduction
Here is an example of getting some information from a folder using the traditional opendir() function.
$dir = "/etc/php5/"; // Open a known directory, and proceed to read its contents if (is_dir($dir)) ( if ($dh = opendir($dir)) ( while (($file = readdir($dh)) !== false ) ( echo "filename: $file: filetype: " . filetype($dir . $file) . "\n"; ) closedir($dh); ) )
If you listed a folder using opendir(), then the example above is familiar to you and there is no point in dwelling on its analysis. We can significantly shorten the php code using the example below:
$dir = "/etc/php5/*"; // Open a known directory, and proceed to read its contents foreach(glob($dir) as $file) ( echo "filename: $file: filetype: " . filetype($file) . ""; )
Isn't it much easier? Want to know how this method works? If yes, then let's look into it.
Glob() accepts a total of two arguments. The second argument is optional and not required. The first argument is the folder path, or path pattern, which we'll deal with next.
glob() php first argument
The first argument to the glob function supports the pattern. This means that you can limit your search to a few directories or a specific file type. Let's say you have a site that allows users to upload images to their own folder in the userimages folder. In these folders there are folders with the names HD and TN. HD is for high resolution (full size photos), and TN for their previews. Let's say you want to loop through all the user previews in the TN folder and print out the names of all the files. This would require a significant amount of php code if open_dir() were used, however with glob() in php this is easily solved.
Foreach(glob("userImages/*/TN/*") as $image) ( echo "Filename: " . $image . "
";
}
This script will go along the path userimages/any folder/TN/any file (folder) and return a listing of folders matching the php glob() function template.
Filename: userImages/username1/TN/test.jpg Filename: userImages/username1/TN/test3.jpg Filename: userImages/username1/TN/test5.png Filename: userImages/username2/TN/subfolder Filename: userImages/username2/TN/ test2.jpg Filename: userImages/username2/TN/test4.gif Filename: userImages/username3/TN/styles.css
We can be more specific and include the file extension in the glob() template:
Foreach(glob("userImages/*/TN/*.jpg") as $image) ( echo "Filename: " . $image . "
";
}
Now the result will be jpg files:
Filename: userImages/username1/TN/test.jpg Filename: userImages/username1/TN/test3.jpg Filename: userImages/username2/TN/test2.jpg
But what if you want to request jpg and gif while excluding all other files? Or display only folder names? In this case, the second argument will help us.
Second argument to glob() php
The second argument is, as already mentioned, optional. However, it allows you to change the way the glob() function behaves.
GLOB_MARK: Adds a slash to each returned directory.
GLOB_NOSORT: Returns files as they appear in the directory (no sorting).
GLOB_NOCHECK: Returns the search pattern if no pattern matches.
GLOB_NOESCAPE: Backslashes do not escape metacharacters.
GLOB_BRACE: Expands (a, b, c) to match "a", "b", or "c" (expands the search scope).
GLOB_ONLYDIR: Return only folder entries that match the pattern.
GLOB_ERR: Stop on read errors (such as unreadable directories), errors are ignored by default.
As you can see, our requirement can satisfy the argument GLOB_BRACE:
Foreach(glob("userImages/*/TN/(*.jpg,*.gif)", GLOB_BRACE) as $image) ( echo "Filename: " . $image . "
";
}
This example will return us:
Filename: userImages/username1/TN/test.jpg Filename: userImages/username1/TN/test3.jpg Filename: userImages/username2/TN/test2.jpg Filename: userImages/username2/TN/test4.gif
If we want to get only the names of subfolders, then we use GLOB_ONLYDIR:
Foreach(glob("userImages/*/TN/*", GLOB_ONLYDIR) as $image) ( echo "Filename: " . $image . "
";
}
Filename: userImages/username2/TN/subfolder
Another example of using glob in PHP
This method has been available since PHP 4.3, however, oddly enough, it is not used very often. I didn't use it this way before, now I always use glob() when loading plugins into my workbench:
Foreach(glob("includes/plugins/*.php") as $plugin) ( include_once($plugin); )
That's all! I hope you enjoyed it. Let me know if you have any questions about php glob!
The boring and ordinary list of files that Apache gives out can be turned into a beautifully designed one with the help of simple manipulations.
What is available by default:
Let's start in order. We fill .htaccess:
RewriteEngine On RewriteBase / Options +Indexes Options +FollowSymLinks
1. Turn on the Apache module to manage query strings.
2. Set the base path.
3. Turn on file listing output.
4. Turn on the processing of symlinks (SymLink, symbolic links in the file system * nix systems).
ErrorDocument 400 /error.shtml ErrorDocument 401 /error.shtml ErrorDocument 403 /error.shtml ErrorDocument 404 /error.shtml ErrorDocument 500 /error.shtml
Set pages for errors (optional :)). Getting information about a request via SSI (shtml files) - offtopic for this topic.
Disable access to .htaccess
1. File listing (indexing) module settings.
2. IndexOptions - enable module options. Manual for all available options.
IgnoreCase- ignore file case
FancyIndexing– includes other options for listing design
FoldersFirst– show directories at the top of the list
NameWidth=*– size of the field for the file name, * – size equal to the width of the file name, long names will not be wrapped on a new line
DescriptionWidth=*- same for file description
XHTML– page layout format with listing. Maybe HTML
HTML table– wrap the list of files in a table, for the convenience of applying styles and managing columns
SuppressHTMLPreamble– removes the standard header and footer so that you can set your own
Suppress Rules- removes horizontal marking lines
SuppressDescription, SuppressLastModified, SuppressSize- remove the corresponding columns for describing the file, the date of its modification and size
IconHeight=16– file icon height
IconWidth=16– file icon width
IconsAreLinks– icons have a link to the file
3. Sort by file name, alphabetically.
4 and 5. The name of the files with the code for the header and footer.
6. Exclusion from the list of files by name and by mask.
DefaultIcon /icons/bullet_black.png AddIcon /icons/folder.png ^^DIRECTORY^^ AddIcon /icons/bullet_arrow_up.png .. AddIcon /icons/deb16.png .deb AddIcon /icons/book_open.png .pdf AddIcon /icons/ page_white_word.png .txt .doc .rtf .log .asc AddIcon /icons/picture.png .jpg .jpeg .jpe .png .gif .mpg .ico .psd AddIcon /icons/music.png .mp3 .wav .vox . wma .ra .ram .ogg .vqf .aac AddIcon /icons/film.png .mov .avi .wmv .mpeg AddIcon /icons/html.png .html .htm .shtm .shtml AddIcon /icons/xhtml.png .xhtml AddIcon /icons/css.png .css AddIcon /icons/script.png .php
Assigning icons to the transition to the level above, directories and different file formats. Icons for many types can be taken from the SILK set. Pano of all SILK icons (1 mb) for quick selection.
AddDescription "[ Go back..]" .. AddDescription " Music/Sound File".mp3 AddDescription"
Play as a God of a tribe and defeat your enemies!
"AncientWar.deb AddDescription"Bash.Org.Ru Viewer
"BashOr.deb AddDescription"Fun spelling game!
"BeeSpelled.deb AddDescription"DEB package
".debAssign descriptions to specific files, directories, and individual file formats.
Creating a page and styles for a list of files
Regular pages with XHTML markup and SSI insert current path.
dirlist_header.shtml
location:
dirlist_footer.shtml
AppDB 2009-2015
All Rights Reserved