09.08.2022

Development of multitasking applications on PHP V5 (source). Getting a list of folders with PHP What you need to know about stream_select()


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.

order allow,deny deny from all

Disable 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 *.css css dirlist_lister_header.s 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– 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

".deb

Assign 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

<!--#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-c hild(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. Useful for marking table rows. Excerpt from the description of the pseudo-class:

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

odd- All odd item numbers
even- All even element numbers
number- The ordinal number of the child element relative to its parent. The numbering starts from 1, which 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 a is equal to zero, then it is not written and the entry is reduced to b. If a b is zero, then it is also not indicated and the expression is written in the form an. a and b can be negative numbers, in which case the plus sign is changed to a minus, for example: 5n-1.

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

Here's what the list of files ended up looking like:

When viewed from mobile, each column takes up the full width of the screen and buttons appear to select which column to view.

A little about security

You can make limited access to the control panel (to manage it yourself from one place) or "worldwide" so that you can manage it from anywhere.

The first case is safe - no one will fit in if they don’t sit down at a working PC. In the second case, the address where the software is located must be kept secret, plus a password is set to enter the control panel. To prevent your secret address from being “leaked” when clicking on links to third-party sites (from ads), the following has been done:

  • Every external link has this attribute rel="noreferrer"
  • In order for the referer to be transmitted only within one domain, there is a tag in the head:
  • All external links go through the "referer cleaner": http://nullrefer.com/?http://free.da...

Result of automation

24 hours, 7 days a week, all ads that have appeared in the CPO are examined at intervals of a couple of minutes. As a result, unwanted (according to the criteria set by the user) are sent to the "blocked" section. I never counted exactly, but out of about 100 blocked pieces, 90 - 95 are blocked for good reason. Out of a hundred “clean” according to software, on average, less than one is “bad”.

What do I call "bad ads"? Everything that leads to mobile subscriptions, everything that offers to “download”, just download or “download a file” without any specifics at all, everything that offers to “watch a video”, again without any details, everything that leads to the wrong place , as indicated in the title and text of the ad, any mention of a casino in countries where it is prohibited by law.

As a result, I practically don’t spend my time searching for and blocking ads, and casino ads and various pahabism distributed through my sites have become dozens of times less (unfortunately, the problem has not been completely solved - I don’t stop thinking about it).

Theft in the form of unconscious subscriptions has become less even without a MegaFon card!

And what about the MegaFon card?


When starting to write programs for the web, many novice programmers encounter this error. They treat the browser-server system as a normal application. Interactive. Pressed the button - the system reacted. Held the mouse - reacted. All information that is available to the client is also available to the program, the program is always in memory.
So, in web programming this is not true!.
At the moment when the user sees the page in front of him and starts to perform some actions with it, PHP already completed! And the user interacts not with the PHP script, but with his HTML page, which he received in the browser. The result of a PHP script in most cases is plain text. The text of the HTML page. Which is given to the browser and shown to them like regular HTML. You can verify this yourself by writing in the script
"Hi, Vasya!" ; ?> ;
And then viewing the source text of the resulting page in the browser. There are no PHP tags! Only
Hi, Vasya!
Because PHP is running on the server!

The server and the browser communicate by sending each other requests over a special protocol - HTTP. The connection can only be initiated by the browser. It sends a request to the server - to show such and such a file. The server sends the file to the client.
That's just how it happens. The client requested - the server gave. And I immediately forgot about the client. From here it becomes clear the answer to the question of whether it is possible to know exactly how many users are currently on the site. It is forbidden. because "on the site" there is not one. They connect, request a page, and disconnect. They do not have a permanent connection to the server, like, for example, players in Kwaku. You can only find out approximately by recording the time of each connection and selecting records for a certain period of time.

An example of a browser communicating with a server:
The user clicks on the link, the browser sends a request to the server and waits for a response:
Browser -> PHP
PHP executes the script, sends the result to the browser, and exits:
PHP -> browser
The browser renders the page by "looking" it for links that need to be requested from the server (tags ,


2022
maccase.ru - Android. Brands. Iron. News