the Internet Windows Android

PHP to get file information. How to create phpinfo file and check PHP information

Any software you want to run on your web server has certain requirements that it must match. For example, WordPress requires PHP version 5.2.4 and higher. Depending on the server, you can change some PHP settings, others on the contrary to change is prohibited, however, you can only get information about the phpinfo file. In this manual, you will learn how to create a PhpInfo file and find out the values \u200b\u200band status of PHP modules. It may also be useful for information about your account hosting, such as max_execution_time, Memory_Limit, Post_max_size and the other.

Before you start this guide, you will need the following:

  • Access to the control panel of your hosting or FTP account

Option 1 - Checking PHP information via hosting control panel

In Hostinger, information about PHP your account can be found in the section Advanced → PHP information. This is a very convenient function, since you will not need to create additional files on your hosting.

After that, you move to the page with all the necessary information about your PHP version, modules and values. To search for a specific module or function, use the search by pressing the keyboard shortcut. Ctrl + F..

Congratulations! You learned how to access information about your PHP through the Hostinger control panel.

Option 2 - Checking PHP information by creating a phpinfo.php file

Do not worry if your hosting platform does not support the feature shown in Option 1.. The same result can be achieved by creating a special file within your account hosting. The file will also show all the information about your PHP and will be available through the browser.

Step 2.1 - Creating a PhpInfo file

There are several ways to create a phpinfo file. In this guide, we will use for this. However, the same result can be achieved by creating a file on your local computer and further loading the file to the server through.

Enter the control panel of your hosting and open File manager. Press the button New file. To create a new file.

In the first field, specify the path where the new file will be created. Next, in the field New file name Enter phpinfo.php.. In most cases, you can leave the path unchanged. / public_html.. In this case, the file will be created in the root directory of your site.

Step 2.2 - Editing File

At this stage you already have a blank file phpinfo.php. in catalog public_html. Copy the following code to the file and click the icon. Savein the left corner of the screen.

That's all, you successfully created a PHP file that displays all the parameters of your PHP. As mentioned earlier, the same result can be achieved with:

  1. Use any text editor and create a file. phpinfo.php. On your computer.
  2. Add the following code to the file:
  1. Download the file using FTP to your catalog public_html.

Step 2.3 - Checking information about PHP through a browser

If everything was done correctly, now you can access the created file by adding to the end of your domain name / P.hpinfo.php.. For instance, http: //vashdomen.ru/phpinfo.php.

You must see a similar result when opening this page through a browser:

On this page you can see all the settings of your PHP.

Conclusion

After completing this manual, you learned how to create a PhpInfo file and check the information about your PHP settings. This information is useful if you want to find out the settings of your hosting or run the software that requires certain PHP modules.

A task
You need to read the file metadata, such as the permissions and the name of the owner.

Decision
Call the STAT () function that returns an array of information about the file:

$ info \u003d STAT ("harpo.php");

Discussion
The STAT () function returns an array of information about the file and with numeric, and with string indexes.

The Mode element of the returned array contains an access rights in the form of an integer basis 10. It confuses, since access rights are usually expressed either symbolically (i.e. output of the LS -RW-R command output), or in the form of octal numbers (i.e. 0644). Convert access rights to a more understandable format allows the BASE_CONVERT () function, transforming access rights to the octal number:

$ file_info \u003d STAT ("/ TMP / session.txt");
$ permissions \u003d base_convert ($ File_info ["Mode"], 10,8);

As a result, we get an octal number of six digits. For example, if the LS command shows about /tMp/session.txt the following:

The value of the value of the $ File_info ["Mode"] is 33204, and the value of the $ permissions variable is 100664.

The last three digits (664) define the access rights to the user: the user (read and write), the group (read and write) and all other (read). The third digit, 0, indicates that the file does not establish a data identifier shift (SETUID), nor the data identifier bits of the group identifier (SETGID). Extremely left number 10 means that this is a regular file (and not socket, symbolic link or other special file).

Since the STAT () function returns an array of both with numeric and with string indexes, then as a result of the Foreach cycle, we obtain two copies of each value. And therefore, you need to refer to the FOR cycle from the element 0 to the element 12 of the returned array.

The FSTAT () function is similar to the considered STAT () and receives a file descriptor (returned by Fopen () or Popen () function) as an argument. The FSTAT () function can be applied only for local files and cannot be used to the URL transmitted FOPEN () functions.
PHP function STAT () exercises expensive basic system call STAT (2).

To minimize PHP's overhead costs, caches the status of the stat (2). Therefore, if you call the STAT () function for the file, change its rights and again call the stat () function for the same file, then the same results will be obtained. To force PHP to overload the metadata file, call the ClearStatCache () function, which will reset the PHP buffer information. The same cache need PHP for other functions returning metadata:

File_exists (), fileatime (), filectime (), filegroup (), fileinode (), filemtime (), FileOwner (),
fileperms (), filesize (), filetype (), fstat (), is_dir (), is_executable (), is_file (), is_link (), is_readable (), is_writable () and lstat ().

Php.

File_exists ("test.txt") // Does the file exist? Filesize ("Test.txt"); // Learning the file size // A temporary label is returned: FileAtime ("test.txt"); // Date of last access to the file // DATE ("D M Y", $ ATIME); filemtime ("test.txt"); // Date of change of file // DATE ("D M y", $ mtime); Filectime ("test.txt"); // Date of file creation (Windows) // DATE ("D M Y", $ CTime);

Files: Modes of operation

Php.

Resource Fopen (String Filename, String Mode) // Resource - Returns a pointer to a file in case of successful work, or false in case of an error
Operating mode Description
R. open file reader only;
R +. open file for reading and writing;
W. Open file only for recording. If it exists, the current content of the file is destroyed. The current position is set to the beginning;
W +. Open the file for reading and writing. If it exists, the current content of the file is destroyed. The current position is set to the beginning;
but Open file for recording. The current position is set to the end of the file;
a +. Open file for reading and writing. The current position is set to the end of the file;
B. Process the binary file. This flag is needed when working with binary files in Windows.

Opening and closing files in PHP

Php.

$ fi \u003d fopen ("test.html", "w +") or die ("error"); // Examples $ fi \u003d fopen ("http://www.you/test.html", "r"); $ fi \u003d fopen ("http://ftp.you/test.html", "r"); // Close FClose ($ FI)

Reading Files in PHP

Php.

// Read the FRED file (Int FI, INT Length) $ str \u003d Fread ($ FI, 5); // Read the first 5 characters echo $ str; // Since the cursor moved $ str \u003d Fread ($ FI, 12); // We read the following 12 characters echo $ str; FGETS (Int FI [, INT LENGTH]) // Read the line from the FGETSS file (Int Fi, Int Length [, String Allowable]) // Read the string from the file and discard HTML tags // String Allowable - tags you want to leave FGETC (Int FI) // reads a symbol from the file

Initially, the record will occur to the beginning of the file by overwriting existing data if they are. Therefore, if you need to write something to the end of the file, you need to install the appropriate reading mode, for example, A +.

Manipulations with the cursor in PHP files

Php.

INT FSEEK (Int FI, INT Offset [, int WHENCE]) // Installing the cursor // Int Fi - pointer to file // offset - the number of characters to which you want to move. // Whence: // Seek_set - the move starts from the beginning of the file; // Seek_cur - the movement comes from the current position; // Seek_end - Movement comes from the end of the file. FSEEK ($ FI, -10, SEEK_END); // We read the last 10 signs of $ s \u003d Fread ($ FI, 10); $ pos \u003d ftell ($ fi); // We learn the current position RewindD ($ F) // Reset the BOOL FEOF cursor ($ F) // End of the File

Direct work with files (data) in PHP

Php.

Array File // Get the contents of the file in the form of an array // Another variant of direct work with the data file_get_contents (String Filename) // Read (we get the entire file with one line) // Record to the file (initially overwritten) file_put_contents (string FileName, Mixed Data [, Int Flag]); // file_append // Record to the end of the file: file_put_contents ("test.txt", "Data", File_append); // if you write an array, $ array \u003d array ("i", "live"); File_Put_Contents ("test.txt", $ array); // then get "ilive"

File Management in PHP

Php.

Copy (String Source, String Destination); // Copy the Rename file (Str OldName, Str Newname); // Rename Unlink file (String FileName); // Delete file.

Loading files to PHP server

// Pressing PHP.ini File_uploads (On | Off) // Allow it. Put the download of files Upload_tmp_dir // Temporary folder for downloaded files. By default, the temporary folder Upload_max_FileSize (default \u003d 2 Mb) // Max. The size of the downloadable POST_MAX_SIZE file // Total size sent shape (there must be more upload_max_filesize) // Simple download

HTML

We work with files on the server

Php.

// We accept the data of $ TMP \u003d $ _files ["userfile"] ["TMP_NAME"]; $ name \u003d $ _files ["userfile"] ["name"]; // Move the Move_uploaded_File file ($ TMP, NAME); Move_uploaded_File ($ TMP, "Upload /". Name); // Redirect the file to the Upload folder // with respect to the current file // that in the $ _files $ _files array ["userfile"] ["name"] // file name, for example, test.html $ _files ["userfile"] [" TMP_NAME "] // Temporary file name (path) $ _files [" userfile "] [" Size "] // File size $ _files [" userfile "] [" type "] // File type $ _Files [" UserFile "] ["error"] // 0 - no errors, the number is

The function collects all the information issued by the operating system for the specified file, and returns it as an array.

Syntax:

Array Stat (String $ FileName)

This array always contains the following elements with specified keys:

0 - device;
1 - Node number inode;
2 - file protection attributes;
3 - the number of synonyms ("hard" links) file;
4 - Owner's UID identifier;
5 - GID group identifier;
6 - type of device;
7 - file size in bytes;
8 - the time of last access in seconds who have passed since January 1, 1970;
9 - the time of the last modification of the contents of the file;
10 - the time of the last change of the attributes of the file;
11 - block size;
12 - the number of busy blocks;

This array places information that is available in UNIX systems. Under Windows, many fields can be empty.

If $ filename does not specify the name of the file, but the name of the symbolic link will be returned, the information on the file referenced will be referenced by this link (and not reference).

fileInode ()

Get Inode File Number (PHP 3, PHP 4, PHP 5)

Description:

Int Fileinode (String FileName)

The function returns the inode file or false in case of error.

Comment

fileowner ()

Get the file owner identifier (PHP 3, PHP 4, PHP 5)

Description:

INT FILEWNER (String FileName)

The function returns a numeric identifier of the owner of the specified file or FALSE in case of error. To get the name of the owner in the form of a string, use the POSIX_GETPWUID () function.

Comment: The results of this function are cached. For more information, see ClearStatCache ().

Starting with PHP 5.0.0, this feature can also be used with some URL packers.

filePerms ()

Get information about the rights to file (PHP 3, PHP 4, PHP 5)

Description:

INT FILEPERMS (String Filename)

The function returns information about the rights to the specified file or false in case of an error occurs.

Comment: The results of this function are cached. For more information, see ClearStatCache ().

Starting with PHP 5.0.0, this feature can also be used with some URL packers. List of packers supported by the family of Stat () functions, see Appendix. M.

The withdrawal of rights in the Emergency Presentation

echo Substr (Sprintf ("% O", Fileperms ("/ TMP")), -4);
echo Substr (Sprintf ("% O", Fileperms ("/ etc / passwd")), -4);
?>

This will output:

1777
0644

Withdrawal of full information about rights

$ perms \u003d fileperms ("/ etc / passwd");

if ((($ perms & 0xc000) \u003d\u003d 0xc000) (
// Sotet.
$ info \u003d "s";
) ELSEIF ((($ perms & 0xa000) \u003d\u003d 0xa000) (
// Symbolic Link
$ info \u003d "l";
) ELSEIF (($ perms & 0x8000) \u003d\u003d 0x8000) (
// Normal
$ info \u003d "-";
) ELSEIF ((($ perms & 0x6000) \u003d\u003d 0x6000) (
// Special Block
$ info \u003d "b";
) ELSEIF ((($ perms & 0x4000) \u003d\u003d 0x4000) (
// Directory
$ info \u003d "D";
) ELSEIF ((($ perms & 0x2000) \u003d\u003d 0x2000) (
// Special symbol
$ info \u003d "C";
) ELSEIF ((($ perms & 0x1000) \u003d\u003d 0x1000) (
// FIFO stream
$ info \u003d "p";
) ELSE (
// Unknown
$ info \u003d "u";
}

// Owner
$ info. \u003d ((($ perms & 0x0100)? "R": "-");
$ info. \u003d (($ perms & 0x0080)? "W": "-");
$ info. \u003d (($ perms & 0x0040)?
(($ perms & 0x0800)? "S": "X"):
((($ perms & 0x0800)? "S": "-"));

// Group
$ info. \u003d ((($ perms & 0x0020)? "R": "-");
$ info. \u003d (($ perms & 0x0010)? "W": "-");
$ info. \u003d (($ perms & 0x0008)?
(($ perms & 0x0400)? "S": "X"):
((($ perms & 0x0400)? "S": "-"));

// Peace
$ info. \u003d ((($ perms & 0x0004)? "R": "-");
$ info. \u003d (($ perms & 0x0002)? "W": "-");
$ info. \u003d (($ perms & 0x0001)?
((($ perms & 0x0200)? "T": "X"):
(($ perms & 0x0200)? "T": "-"));

echo $ info;
?>

Result:

R - R - R--

fnmatch ()

Does the file name with the template matches (PHP 4\u003e \u003d 4.3.0, PHP 5)

Description:

BOOL FNMATCH (String Pattern, String String [, Int Flags])

fnmatch () Checks whether the transmitted String parameter matches with the specified shell wildcard template (Shell Wildcard). Pattern.

This feature is useful when working with file names, although it can be used on a conventional string. The average user is familiar with the shell substitutions, at least in their simplest form from the substitutions "?" and "*", so using fnmatch () instead of Ereg () or preg_match () to search in the user part of the site can be much more convenient for users who are not familiar with regular expressions.

Checks if the color of the shell substitution matches.

if (Fnmatch ("* Gry", $ color)) (
echo "Some Form of Gray ...";
}
?>

At the moment, this feature is not available in Windows and other POSIX incompatible systems.

Gets File Information Using Open File Pointer (PHP 4, PHP 5)

Description:

Array Fstat (Resource Handle)

Collects statistical information about the open file on the Handle file pointer. This feature is similar to STAT (), except that it works with an open file pointer, and not the file name.

Returns an array with statistical information on the file; The array format is described in detail on the Stat () function page.

Example of using the FSTAT () function

// Open the file
$ FP \u003d Fopen ("/ etc / passwd", "r");

// Collect statistics
$ FSTAT \u003d FSTAT ($ FP);

// Close File
fClose ($ FP);

// Displays only the associative part
print_R (Array_Slice ($ FSTAT, 13));

The result of this example will be something like this:

Array.
=> 771
=> 488704
=> 33188
=> 1
=> 0
=> 0
=> 0
=> 1114
=> 1061067181
=> 1056136526
=> 1056136526
=> 4096
=> 8
)

Comment: This feature is not applicable to work with remote files, since the file must be available through the server file system.

iS_WRITABLE ()

Determines whether the file for recording is available (PHP 4, PHP 5)

Description:

BOOL IS_WRITABLE (STRING FILENAME)

Returns True if the filename file exists and is available for recording. The FileName argument may be the name of the directory, which allows you to check the directory for accessibility.

Do not forget that PHP can access the file on behalf of the user from which the web server is running (usually "NOBODY"). The restrictions of the safe regime are not taken into account.

Comment: The results of this function are cached. For more information, see ClearStatCache ().

Starting with PHP 5.0.0, this feature can also be used with some URL packers.

Gets file information or symbolic link (PHP 3\u003e \u003d 3.0.4, PHP 4, PHP 5)

Description:

Array Lstat (String Filename)

Collects statistics to a file or a symbolic link named FileName. This function is identical to the STAT () function, except that if the filename is a symbolic reference, the status of a symbolic link is returned, and the wrong file to which it indicates.

Refer to the Stat () function page to obtain information about the structure of the array that returns LSTAT ().

Comment: The results of this function are cached. For more information, see ClearStatCache ().

Starting with PHP 5.0.0, this feature can also be used with some URL packers.

fileAtime.

Returns the last file access time.

Syntax:

Int FileAtime (String Filename)

If the file is not detected, returns FALSE.

The time attribute of the last access to the file varies each time the file of the file is read. Since this greatly reduces performance with intensive work with files and directories, often changing this attribute in operating systems is blocked, and then the function is useless.

filemtime.

Returns the time of the last file change or false in the absence of a file.

Syntax:

Int FileMtime (String $ FileName)

filectime.

Returns the file creation time.

Syntax:

INT FILEctime (String $ FileName)

filesize

Returns the file size in bytes or false if the file does not exist.

Syntax:

INT FILESIZE (String $ FileName)

Sets the modification time.

Syntax:

Int Touch (String $ FileName [, int $ timestamp])

Sets the modification time of the specified $ filename file to $ TimeStamp (in seconds that have passed since January 1, 1970).

If the second parameter is not specified, then the current time is meant. In case of an error, returns FALSE.

If the file with the specified name does not exist, it is created empty.

Changes the file access mode or catalog (PHP3, PHP4, PHP5)

Description:

BOOL CHMOD (String Filename, Int Mode)

Exercises an attempt to change the file access mode or directory transmitted in the FileName parameter to the mode transmitted in the Mode parameter.

Please note that the value of the MODE parameter is not automatically translated into an octal number system, therefore rows (such as, for example, "G + W") will not work properly. To make sure that the mode has been set correctly, predicted the value transmitted in the Mode parameter, zero (0):

chmod ("/ Somedir / SomeFile", 755); // decimal, wrong way
cHMOD ("/ Somedir / SomeFile", "U + RWX, GO + RX"); // Row, wrong way
chmod ("/ somedir / somefile", 0755); // octal, true way
?>

The value of the MODE parameter consists of three octal numbers defining the level of access to the owner of the file, for the group in which the owner includes, and for other users, respectively. The number defining the level of the user can be calculated by summing the values \u200b\u200bdefining the rights:

1 - access to execution
2 - write access,
4 - read access.

You can learn about the system of rights in UNIX systems using the "MAN 1 CHMOD" and "man 2 chmod" commands.

// Access to record and reading for the owner, no access for others
chmod ("/ somedir / somefile", 0600);
// Record access and reading for the owner, reading for others
cHMOD ("/ Somedir / SomeFile", 0644);
// full access for the owner, reading and executing for others
chmod ("/ somedir / somefile", 0755);
// Full access to the owner, read access and execution for the owner group
chmod ("/ Somedir / SomeFile", 0750);
?>

Returns TRUE in case of successful completion or false in case of error.

Note: The current user is the user, on behalf of the PHP. It is possible that this user will be different from the user, under the name of which you get access to the command shell or FTP account.

Note: This feature is not applicable to work with remote files, since the file must be available through the server file system.

Note: When a secure mode is enabled, PHP checks whether the file or directory with which you are working, the same UID, as well as the script being executed. In addition, you cannot install Suid, Sgid and "sticky" bits.

(PHP 3, PHP 4, PHP 5)

stat - Gets file information

Description

Array. stat (String FileName)

Collects statistical information about the FileName file. If the filename is a symbolic link, the information is about the file itself, and not the link. Function lstat () Extra function sTAT () Except that in this case it will return information about the most symbolic link.

In case of error, sTAT () return False . She will also give a warning.

Returns an array of statistical information about the file. The array includes the following elements. Indexing begins with scratch. Also, access to any attribute can be obtained by an associative key, which is indicated opposite each parameter; This feature appeared from the PHP version 4.0.6:

Table 1. Format of the result of the function of functions sTAT () and fSTAT ()

* - Available only on systems that support the ST_BLKSIZE type, other systems (for example, Windows) are returned -1.

Prompt:Starting with PHP 5.0.0, this feature can also be used with some URL packers. List of packers supported by family functions sTAT (), look in