/ / Php. Working with files and directories

PHP. Working with files and directories

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.

Basic principles of work

Databases are convenient, but every developer shouldat 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. "

Learning php

So, here are a few reasons why you might need files:

  1. To move binary data (for example, image files) to the BLOB database (binary large objects).
  2. Import data (e.g., email addresses) exported from an outdated database or application.
  3. To export from the database information to a text file for offline processing.

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:

  • about the size;
  • give information about when the last time he was approached;
  • tell about the owner, etc.

It is best to learn all the basic attributes for working with files in PHP. This will greatly facilitate the work.

File history

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 ().

Programming with php
 ";
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:

  • C: Windowsfile.ini was viewed on Sep 19, 2018 4:34 pm.
  • C: Windowsfile.ini was changed Fri Oct 8, 2018 2:03 am.
  • C: Windowsfil.ini was changed in Tues Dec 16, 2017 4:34.

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.

File or not?

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.

";
echo $ file. (is_dir ($ file)? "": "not"). "directory.";

Example code output:

  • C: Windowsfile.ini file.
  • C: Windowsfile.ini is not a directory.

Thus, you can avoid mistakes and do not open by mistake "not a file". In PHP, work with files and directories is similar.

File permissions

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.

";
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 will display the following values:

  • C: Windowsfile.ini is read.
  • C: Windowsfile.ini is not written.

Using the ternary operator, you can specify whether the file is available or not.

file size

To find out the file size, you need to use the filesize () function. It will be displayed in bytes.

Working with php

The function will display the following:

  • C: Windowsfile.ini is 510 bytes in size.

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 ().

The file_exists () should be checked almost always for security.

Reading Files

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.

Work in php with ini files

Functions for working with PHP files make it easy.In this case, you will need file_get_contents (). It will read the entire contents of the file into a variable without the need to open or close the file itself. This is convenient when the recording volume is relatively small, since immediately reading 1 GB of data into the archive is not always rational in PHP. Working with “.ini” files and the file_get_contents () function is shown below.

For large files or just depending onneeds of your script, it may be wiser to handle the parts yourself. This is due to the fact that as soon as a file is opened, you can search for a specific note in it and read as much data as you want. The fopen () function is used to open a file.

Two arguments are required to work with the fopen () function:

  • the file to open;
  • The mode used in this case is “r” for reading.

The function returns a handle or stream to a file, which is stored in the $ file1 variable. It should be used in all subsequent commands when working with a file.

The most common mode values
Mode Value Cursor position If the file does not exist?
R only reading file start will give an error
at only record file start will create a new
a only record end of file will create a new

In order to read from an open file one line at a time, you can use the fgets () function.

";}
while (! feof ($ file1));
fclose ($ file1);

Using the do-while loop is goodchoice to know in advance how many lines are in a file. The feof () function checks whether the file has reached completion, and the cycle continues until the end of the file condition is reached. After reading is finished, the fclose () function is used to close the document.

Write files

Two commonly used modes when writing to a file withusing the fwrite () function: “w” and “a”. “W” means that you need to write to the document, but it will first remove any content, “a” - add new data to what already exists in the file. You need to be sure that the correct version is used.

Work with php and ini files

The following example will use “a” mode for recording.

First, the file name is assigned to the variable, thenIt opens in “a” mode to add. The data to be written is assigned to the variable $ 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 constant PHP_EOL adds a newline character specific to the platform on which PHP works with text files.

The contents of the file after the execution of the above code should look like this:

  • banana;
  • China.

The file_put_contents () function can alsowrite to file. It takes the file name, the data to be written, and the FILE_APPEND constant if it is to add data (will overwrite the contents of the file by default).

Here is the same example as above, but this time using file_put_contents ().

You have to work with these functions often, so it’s better to remember them. In addition, they can in the work with PHP files once to facilitate some complex tasks.