Save up to 80 % on our Swift e-learning courses. Only this week!
Get up to 80 % extra points for free! More info
Summer BF

Lesson 1 - Introduction to work with files in PHP - Online course New

Welcome to the first lesson of the course about files in PHP. In this course, we will learn how to read, write and process different types of files in PHP. But first, we need to get a theoretical foundation and clarify some concepts.

Why do we need files?

All data contained in variables are stored in RAM. The big advantage is that they are read and written very quickly, but when the script terminates, they're lost forever. What if we're creating e.g. a guest book and need to keep the records? We use files for permanent data storage.

File types

We recognise two types of files:

  • text and
  • binary

Text files

In text files, the data is stored in a human-readable form and the file can be opened in text editors (e.g. in Notepad). Text files can sometimes be machine-processed. In that case, we're talking about structured text files. Otherwise, the files are unstructured.

Structured text files

The content of structured files is arranged in a way, that makes it possible for the program to interpret it. We distinguish between flat files and hierarchical files. We choose between these types accordingly to the "complexity" of the data that we want to store. PHP can work with all of the formats mentioned below using the built-in functions.

Flat files vs. hierarchical

Between the simple forms of structured files, we find Flat files, where we usually work with delimiters that separate the individual values of primitive data types (numbers, boolean true/false, strings). If we also want to store tree data structures in files, such as arrays or objects, we must use hierarchical files. Here, the way data is stored varies quite a bit between formats.

CSV files

The most commonly used flat file format is CSV (Comma Separated Values). We use commas or semicolons as delimiters and we put individual records on separate lines:

"Dave Brown",21,Programmer
"Catherine Smith",45,Café Waitress
"Thomas Adams",27,Teacher
XML files

The hierarchical XML format (eXtensible Markup Language) seems noticeably similar to structure of a HTML document. Nowadays, it's often being replaced by the JSON format, which is shorter and simpler:

<?xml version="1.0" encoding="UTF-8" ?>
<people>
    <person>
        <name>Dave Brown</name>
        <age>21</age>
        <occupation>Programmer</occupation>
    </person>
    <person>
        <name>Catherine Smith</name>
        <age>45</age>
        <occupation>Café Waitress</occupation>
    </person>
    <person>
        <name>Thomas Adams</name>
        <age>27</age>
        <occupation>Teacher</occupation>
    </person>
</people>
JSON files

JSON (Java Script Object Notation) is probably the most widely used hierarchical file format today. The name of the format already suggests that the notation of JSON is identical to the notation of objects in JavaScript. Non-ASCII characters in strings must be escaped, as we can see in the example. This is to ensure better portability:

[
    {
        "name": "Dave Brown",
        "age": 21,
        "occupation": "Programmer"
    },
    {
        "name": "Catherine Smith",
        "age": 45,
        "occupation": "Caf\u00e9 Waitress"
    },
    {
        "name": "Thomas Adams",
        "age": 27,
        "occupation": "Teacher"
    }
]

Unstructured text files

Unstructured text files, also called plain text files, are readable only by humans and cannot be processed programmatically. For example, we refer to a letter written in Notepad. The extension of these files is .txt by default:

Hi, my name is Paul and I am 26 years old. I work as a programmer in a certain London company. Programming is also my biggest hobby. Besides, I like to swim and watch TV series. Do you think we could ever meet? Have a nice day, Paul

Binaries

We can also store data in binary form. In a nutshell, it is only a print of a portion of RAM. These files are not human readable, cannot be opened with a text editor and are always structured. For example, pictures and videos are stored on the disk in this form. In PHP, we rarely encounter the processing of these files, so in this lesson, we won't learn about them in detail.

Paths and file names

As we know, files are stored in folders on a disk. Folders can contain subfolders, creating a hierarchical/tree structure. When working with files, we will have to give PHP information where (in which folder) to look for a certain file. This is achieved by passing a path leading to the required file to a function that will perform some activity with the file (reading, writing,…). These paths can be absolute or relative.

Relative paths

As the name indicates, the relative path to the file will be related to something. In the case of PHP, the relative path is always related to the folder in which our script is located:

file.txt
resource-folder/file.txt
../file.txt
../another-folder/file.txt

In the first and simplest example, we work with a file named file.txt in the current folder, in the second one we work with a file located in a folder resource-folder (which is in the current folder). In the third example, we work with a file in the hierarchy one folder up and in the last one, with a file, which is located in the another-folder folder, which is one folder above the current one.

You may have noticed, that we use normal slashes (/) as folder separators and not backslashes (\), as it is common in Windows. In most cases, web hosting and servers, in general, have the Linux operating system, where classic slashes are used to separate folders and backslashes cannot be used. But it works vice-versa.:-)

If you don't want to worry too much about it, the most important thing to know is that if we refer to the file only by its name, PHP will look for it in the folder where we have the PHP script loaded.

Absolute paths

These paths are not rooted in a folder with our script and we write them in their entirety, from the root folder (in the case of Windows it's the drive letter, in the case of Linux, it's the / folder). We will mention this type of path here only for completeness, because I would not recommend using it in PHP. We can move our website between different servers, where the absolute path to the folder with our website will be completely different and that would unnecessarily cause us problems. After moving our website, our PHP scripts could look for the file somewhere, where it's not or, in the worst case, in some foreign space.:) The absolute path can look like this:

/var/www/html/myweb.com/file.txt
C:\Apache2\htdocs\myweb.cz\file.txt

That's all for today's lesson:)

In the next lesson, Working with text files in PHP, we'll show you how to read and write (not only) unstructured text files.


 

All articles in this section
Working with files in PHP
Article has been written for you by Jan Štěch
Avatar
Do you like this article?
No one has rated this quite yet, be the first one!
As an author I'm interested in PHP for web development, I'm also trying to improve in C#
Activities (5)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!