Anemic listing php. Getting a list of folders using PHP


Reg.ru: domains and hosting

The largest registrar and hosting provider in Russia.

More than 2 million domain names in service.

Promotion, domain mail, business solutions.

More than 700 thousand customers around the world have already made their choice.

*Mouse over to pause scrolling.

Back forward

Getting a list of folders using PHP

Listing directories using PHP, or listing directories

We will call directory listing a general query that generates a list of all or some files and directories of the parent directory - a process similar to the work of the index page provided by most web servers, but with a O Greater control over the content and formatting of the same.

Another advantage of this script is the ability to perform certain actions with files using PHP. In any case, the first step we need to do is query the file system - return a list of files and directories.

The functions presented below allow you to extract file names and other properties from a specific directory, or iterate through subcategories recursively.

Comment: PHP5 has a function scandir, which “returns a list of files and directories inside a directory at a given path,” but it does not display any additional information about the files inside the directory.

Listing one directory

To begin with, here is an example of a simple function that returns a list of files, directories and their properties from one directory (you will find more advanced versions of this function a little later in this lesson.)

read())) ( // skip hidden files if($entry == ".") continue; if(is_dir("$dir$entry")) ( $retval = array("name" => "$dir$ entry/", "size" => 0, "lastmod" => filemtime("$dir$entry")); ) elseif(is_readable("$dir$entry")) ( $retval = array("name" = > "$dir$entry", "size" => filesize("$dir$entry"), "lastmod" => filemtime("$dir$entry")); ) ) $d->close(); return $retval; ) ?>

You can use this function like below:

The return value is an associative array of files, including the file's path information, size, and last modification date, unless the file is a directory, in which case the string "(dir)" appears instead of the file size.

Example 1:

",print_r($dirlist),""; /* example output Array ( => Array ( => images/background0.jpg => image/jpeg => 86920 => 1077461701) => ...) */ ?>

Example 2:

",print_r($dirlist),""; /* example output Array ( => Array ( => ./images/background0.jpg => image/jpeg => 86920 => 1077461701) => ...) */ ?>

Listing files via HTML

To get the output results on the page in HTML, we'll loop through the returned array

\n"; echo " NameTypeSizeLast Mod.\n"; foreach($dirlist as $file) ( echo " \n"; echo " ($file["name"])\n"; echo " ($file["type"])\n"; echo " ($file["size"])\n"; echo " \n"; echo "\n"; ) echo "\n\n"; ?>

This code is quite easy to modify, for example:

  • - display the listing results as a list instead of a table;
  • - make file names active links;
  • - replace names with icons based on what type of file it is;
  • etc.

For example, to output only PNG files, add a simple condition to the output loop:

\n"; echo " NameTypeSizeLast Mod.\n"; foreach($dirlist as $file) ( //check if the file is a PNG if(!preg_match("/\.png$/", $file["name"])) continue; echo " \n"; echo " ($file["name"])\n"; echo " ($file["type"])\n"; echo " ($file["size"])\n"; echo " ",date("r", $file["lastmod"]),"\n"; echo "\n"; ) echo "\n\n"; ?>

This example will skip and hide all files whose names end with .png. You can also apply additional conditions based on the file type, size, or date it was last modified.

If you, for example, want to display a thumbnail, a link to a larger picture, or even a video, just give these 2 files the same names and in the script above use str_replace or similar function to modify the content of links.

Recursive directory listing

And since we've come this far, there will only be minor changes to the recursive list and subcategory calling function. By adding a second parameter to the function, we retain the previous functionality of listing a single directory.

read())) ( // skip hidden files if($entry == ".") continue; if(is_dir("$dir$entry")) ( $retval = array("name" => "$dir$ entry/", "size" => 0, "lastmod" => filemtime("$dir$entry")); if($recurse && is_readable("$dir$entry/")) ( $retval = array_merge($ retval, getFileList("$dir$entry/", true)); ) ) elseif(is_readable("$dir$entry")) ( $retval = array("name" => "$dir$entry", "size " => filesize("$dir$entry"), "lastmod" => filemtime("$dir$entry")); ) ) $d->close(); return $retval; ) ?>

For the new functionality to work, you need to enter a value true(or 1) as the second parameter.

Before recursing the script, check whether the subdirectories are readable and also read the last paragraph of this lesson in order to avoid access errors.

As before, the return value is an array, an associative array. In fact, the only addition is another additional option for recursive listing.

Recursion Depth Limit

This final example adds one more feature - the ability to specify how "deep" the recursion should go. The previous code will continue to explore subdirectories until they run out. This script will help you set a limit on the number of levels of nested directories.

read())) ( // skip hidden files if($entry == ".") continue; if(is_dir("$dir$entry")) ( $retval = array("name" => "$dir$ entry/", "size" => 0, "lastmod" => filemtime("$dir$entry")); if($recurse && is_readable("$dir$entry/")) ( if($depth == = false) ( $retval = array_merge($retval, getFileList("$dir$entry/", true)); ) elseif($depth > 0) ( $retval = array_merge($retval, getFileList("$dir$entry /", true, $depth-1)); ) ) ) elseif(is_readable("$dir$entry")) ( $retval = array("name" => "$dir$entry", "size" => filesize("$dir$entry"), "lastmod" => filemtime("$dir$entry")); ) ) $d->close(); return $retval; ) ?>

As before, we added only 1 new parameter and a couple of lines of code. If the default value responsible for the recursion depth is not specified, then it is set to false. This allows us to be sure that previous features remain and subsequent code will not “break” when the function changes.

Doing business on Amazon involves learning various terminology that is found only on this online resource. Search engine optimization is important and should not be forgotten. Today we'll talk about listings and their role in the process of selling products on Amazon.

Let's start by defining the meaning of this word. A listing is a page with a description and characteristics of a product in an online store. Every seller should be clearly aware of the importance of filling out this field and not ignore this stage. Completing a product listing on Amazon involves several steps.

1. Product title.

Filling out this field includes the name of the product. This is the first thing a customer will see when they land on your product page. For a headline to be good, it must contain the name of the product and its most important characteristics (for example, weight, size, TM). It is best to use the most popular search queries in the title. This will help your product appear in the first positions in searches.

2. Product photo.

As they say, you meet someone by their clothes. On Amazon, products are also rated based on their photos. The listing must contain only high-quality photographs that show the product at its best. The main photo should show a close-up of the product, and the rest should show the product in action.

3. Bullets.

In other words, these are the key advantages of this product, which are presented in the form of an unnumbered list. Bullets should be filled with information that will help the buyer make a positive decision regarding this product.

4. Product description.

Here you need to write everything that was not covered in the bulletins. You can add more information about the brand, about certificates that confirm the quality of the product. The main thing in the description should be highlighted in CAPITAL LETTERS. Thanks to this trick, everything you need will immediately catch your eye.

5. Key words.

Keys in listings are very important, but too many of them can discourage the buyer from any desire to purchase the product. Compose keywords correctly without violating the design of the listing.

If the product does not match the description in the listing, the buyer can leave a complaint and when the required number of votes is collected, Amazon blocks the account. Listings have the ability to create different variations of an item, depending on color, quantity, and other factors. It is recommended to create different listings for different products.

A high-quality and good listing should be concise and informative. In several sentences the entire essence, purpose of the product and its main characteristics should be fully disclosed. The right listing will help attract buyer attention and generate profit.

The boring and ordinary list of files that Apache produces can be turned into a beautifully designed one with the help of simple manipulations.

What's available by default:

Let's start in order. Fill in .htaccess:

RewriteEngine On RewriteBase / Options +Indexes Options +FollowSymLinks

1. Enable the Apache module to manage query strings.
2. Set the base path.
3. Enable file listing output.
4. Enable processing of symlinks (SymLink, symbolic links in the file system of *nix systems).

ErrorDocument 400 /error.shtml ErrorDocument 401 /error.shtml ErrorDocument 403 /error.shtml ErrorDocument 404 /error.shtml ErrorDocument 500 /error.shtml

We set pages for errors (optional :)). Receiving information about a request via SSI (shtml files) is an off-topic for this topic.

order allow, deny deny from all

We prohibit access to .htaccess

IndexOptions IgnoreCase FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=* XHTML HTMLtable SuppressHTMLPreamble SuppressRules SuppressLastModified IconHeight=16 IconWidth=16 IndexOrderDefault Ascending Name HeaderName dirlist_header.shtml ReadmeName dirlist_footer.shtml IndexIgnore error.shtml *.png *.cs s dirlist_header.shtml dirlist_footer.shtml cgi- bin favicon.ico .htaccess .ftpquota .DS_Store *.log *,v *,t .??* *~ *#

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– display directories at the top of the list
NameWidth=*– the size of the field for the file name, * – the size is equal to the width of the file name, long names will not be wrapped on a new line
DescriptionWidth=*– the same for the file description
XHTML– format of the page layout with the listing. Maybe HTML
HTMLtable– wrap the list of files into a table for ease of applying styles and managing columns
SuppressHTMLPreamble– removes the standard header and footer so you can set your own
SuppressRules– removes horizontal marking lines
SuppressDescription, SuppressLastModified, SuppressSize– remove the columns for the file description, modification date and size accordingly
IconHeight=16– file icon height
IconWidth=16– file icon width
IconsAreLinks– icons have a link to the file
3. Sorting by file name, alphabetically.
4 and 5. Names of files with code for header and footer.
6. Exclusion from the list of files by name and 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 navigation to a higher level, 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

".deb

Assigning descriptions to specific files, directories, and individual file formats.

Creating a page and styles for a list of files
Regular pages with XHTML and SSI markup inserting the current path.

dirlist_header.shtml

<!--#echo var="Request_URI" -->

Location:


dirlist_footer.shtml


AppDB 2009-2015
All Rights Reserved

style.css

Body ( background-attachment: fixed; ) #wrap ( width:960px; margin:30px auto 0; ) #main ( width:900px; float:left; padding:10px 30px 0 30px; border:0px; ) #tbl ( border :1px dashed #555; padding: 0; ) h1 ( font: 2.0em Verdana, Georgia, serif; text-align:center; color:#787878; ) h3.location ( font-size:13px; font-weight: bold ; margin:12px 0 30px; text-align:center; color:#4D4D4D; ) a:link, a:visited ( text-decoration: none; color: #aaa; ) a:hover, a:active ( text-decoration : none; color: #eee; border-bottom: 1px dashed #eee; ) table ( width:100%; margin:0; margin-bottom:10px; border:0; ) tr ( width:100%; padding:0 ; margin:0; ) tr:nth-child(odd) ( background: url(181818.png); ) tr:nth-child(even) ( background: url(222222.png); ) tr:nth-child( odd):hover td, tr:nth-child(odd):active td, tr:nth-child(even):hover td, tr:nth-child(even):active td ( color: #eee; ) tr: nth-child(odd):hover td a, tr:nth-child(odd):active td a, tr:nth-child(even):hover td a, tr:nth-child(even):active td a ( color: #eee; ) th ( display:none; ) td ( height:20px; padding:20px 10px 10px 20px; margin:0; ) td:nth-child(1)( width:16px; ) hr ( display:none; ) .description ( margin:0; padding-right:15px; text-align:left; )

It is worth noting the CSS:nth-child() pseudo-class, it allows you to set the style for child elements. Convenient for marking table rows. Excerpt from the description of the pseudo-class:

Element: nth-child(odd | even | |) (...)

odd- All odd numbered elements
even- All even numbered elements
number- The ordinal number of the child element relative to its parent. Numbering starts from 1, this will be the first element in the list.
expression- Specified as an+b, where a and b are integers, and n is a counter that automatically takes the value 0, 1, 2...

If a equals zero, then it is not written and the entry is reduced to b. If b is equal to zero, then it is also not indicated and the expression is written in the form an. a And b can be negative numbers, in this case the plus sign changes to a minus, for example: 5n-1.

Examples of XHTML and CSS files taken from apt.appdb.ru.

This is what the list of files ended up looking like:







2024 gtavrl.ru.