The databases MySQL and Access are increasingly becomingaccessible means of data storage. But in the early 1990s, it was popular to work with files in PHP, saving records in formatted text files in CSV format, separated by new lines.
Базы данных удобны, но каждый разработчик должен at least have some basic knowledge about how to read and write files. Perhaps many will reflect on the question: "Why do I need to know this?" If I use files, they are written in XML, and I just use the parser. "
So, here are a few reasons why you might need files:
Reading files and writing are basic operations.If you want to read the document, you first need to open it. After that, read as much content as possible, then close the file. To write information to a document, you must first open it (or, perhaps, create it, if it is not already created). After that, record the necessary data and close it at the end.
It is also convenient to use built-in functions that automatically open and close. They are available in PHP5. You should also familiarize yourself with the attributes of the files, that is, with its properties.
They can tell:
It is best to learn all the basic attributes for working with files in PHP. This will greatly facilitate the work.
You may need to know the time of the last editing of the file. In this case, the functions come to the rescue: fileatime (), filemtime (), and filectime ().
";
echo $ file. "had the last i-node change". date ($ formatDate, $ timeM). "."
";
echo $ file. " it was changed " . date ($ formatDate, $ timeC). "."; ";
Here, the code retrieves the timestamp of the last access and displays it:
The function filectime () shows the time of change of various information associated with the file (for example, access rights), and filemtime () - changes the file itself.
The date () function was used to format the Unix timestamp returned by file * time () functions.
To find out whether it really works with files in PHP, you can use the is_file () function or is_dir () to check if it is a directory.
php
echo $ file. (is_file ($ file)? "": "not"). "file.
";
echo $ file. (is_dir ($ file)? "": "not"). "directory.";
Example code output:
Thus, you can avoid mistakes and do not open by mistake "not a file". In PHP, work with files and directories is similar.
Before working with a file, you can check whether it is readable or writable. To do this, use the is_writable () and is_readable () functions.
php
echo $ file. (is_readable ($ file)? "": "not"). "read.
";
echo $ file. (is_writable ($ file)? "": "not"). "is recorded.";
These functions return a Boolean value and explain whether the operation can be performed in a file.
The code displays the following values on the screen:
Using the ternary operator, you can specify whether the file is available or not.
To find out the file size, you need to use the filesize () function. It will be displayed in bytes.
php
$ file = "C: Windowsfile.ini"; $ size = filesize ($ file);
echo $ file. "has a size". $ size. "byte.";
The function will display the following:
Use the file on the Windows system herestresses one nuance. The backslash has a special meaning as an escape character. It will be necessary to avoid this by adding one more backslash.
If the file is not already created, the function filesize () will point to False and an error. Therefore, first check the file for the existence of the necessary command file_exists ().
php
$ file = "C: Windowsfile.ini"; if (file_exists ($ file)) {$ size = filesize ($ file);
echo $ file. "has a size". $ size. "byte.";} else {echo $ file. " file not found.";}
The file_exists () should be checked almost always for security.
The previous section shows how much you can doLearn about the files that you work with before you start reading or writing to them. Now you can disassemble how the contents of the file are read.
Functions for working with PHP files make the task easier.In this case, you need file_get_contents (). It will read the entire contents of the file into a variable without having to open or close the file yourself. This is convenient when the amount of write is relatively small, since immediately reading 1 GB of data into an archive is not always rational in PHP. Working with ".ini" files and file_get_contents () is shown below.
php
$ file = "c: windowsfile.ini"; $ file1 = file_get_contents ($ file);
echo $ file1;
For large files or simply depending on theneeds of your script, it may be more reasonable to process the details yourself. This is due to the fact that as soon as the file is opened, you can search for a specific comment in it and read as much data as you want. The fopen () function is used to open a file.
php
$ file = "c: windowsfile.ini"; $ file1 = fopen ($ file, "r");
To work with fopen (), you need two arguments:
The function returns a descriptor or stream to a file that is stored in the variable $ file1. It should be used in all subsequent commands when working with a file.
Mode | Value | Cursor position | If the file does not exist? |
R | only reading | beginning of file | will give an error |
at | only record | beginning of file | create a new |
a | only record | end of file | create a new |
To read from an open file one line at a time, you can use the fgets () function.
php
$ file = "c: windowsfile.ini"; $ file1 = fopen ($ file, "r");
do {echo fgets ($ file1). "
";}
while (! feof ($ file1));
fclose ($ file1);
Using a do-while-loop is goodchoice to find out in advance how many lines in the file. The feof () function checks whether the completion file has reached, and the loop continues until the end of the file condition is reached. After the end of the reading, the fclose () function is used to close the document.
Two frequently used modes when writing to a file withusing the function fwrite (): "w" and "a". "W" means what you want to write to the document, but it will first delete any content, "a" - adding new data to what already exists in the file. You need to be sure that the correct version is used.
The following example will use the "a" mode for writing.
php
$ myFile = "files.txt"; $ file1 = fopen ($ myFile, "a");
$ output = "banana". PHP_EOL; fwrite ($ file1, $ output);
$ output = "China". PHP_EOL;
fwrite ($ file1, $ output);
fclose ($ file1);
First, the file name is assigned to a variable, thenit opens in the "a" mode to add. The data to be written is assigned to $ output and fwrite (), and the information is added to the file. The process is repeated to add another line, then the document is closed using fclose ().
The predefined PHP_EOL constant adds a newline character specific to the platform on which PHP runs with text files.
The contents of the file after executing the above code should look like this:
The file_put_contents () function can alsowrite to file. It takes the name of the file, the data that must be written and the constant FILE_APPEND, if it should add data (it will overwrite the contents of the file by default).
Here is the same example as above, but this time using file_put_contents ().
php
$ myFile = "files.txt";
file_put_contents ($ myFile, "banana." PHP_EOL);
file_put_contents ($ myFile, "China." PHP_EOL, FILE_APPEND);
With these functions you have to work often, so it's better to remember them. In addition, they can sometimes work with PHP files to alleviate some complex tasks.