/ / 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

Базы данных удобны, но каждый разработчик должен 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. "

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 displays the following values ​​on the screen:

  • 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 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.

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.

To work with fopen (), you need two arguments:

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

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.

The most common mode values
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.

";}
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.

Writing files

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.

Working with php and ini files

The following example will use the "a" mode for writing.

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:

  • banana;
  • China.

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

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.

Liked:
0
© 2018, "Paulturner-Mitchell.com". Use of any materials posted on the site is permitted provided that the link to "Paulturner-Mitchell.com".
yup