P
P
PO6OT2015-11-25 16:23:10
PHP
PO6OT, 2015-11-25 16:23:10

How to deal with large files in PHP?

Sample tasks:
1. Change the first character in a file without loading the entire file into memory.
2. Remove the first character without loading the entire file into memory.
3. Add 1 character to the beginning of the file without loading the entire file into memory.
With small files, everything is simple:

//1
file_put_contents($file, $symbol.substr(file_get_contents($file), 1));
//2
file_put_contents($file, substr(file_get_contents($file), 1));
//3
file_put_contents($file, $symbol.file_get_contents($file));

But the problem is that the file is large and it is not worth loading it into memory, since it needs to be changed often.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton B, 2015-11-25
@bigton

fopen, fread, fwrite

R
Roman Mirilaczvili, 2015-11-25
@2ord

Read and write information from/to a file in blocks of a fixed size of N bytes (to a buffer).
When appending to the end of a file, all blocks from the old file are first copied to the new one, then a block of the required character length is added. The file is closed and renamed to the name of the old one.
When deleting information, you need to calculate how many blocks of the file you need to skip first, then copy the remaining information in blocks to a new file, close and rename.
If you need to change only a certain sequence of bytes without changing the length of this sequence, then you only need to change the values ​​in the buffer of a certain block to one another and write the block buffer to a new file, and copy the remaining blocks unchanged.
Note that a "character" can be 1 byte or larger. I advise you to pay attention to how the character is encoded in UTF-8 and others.
The simplest case is when a "character" is encoded with only one byte. 1 byte is read from the source file, replaced in the same position with a byte of another value. Everything.
If, say, a Cyrillic character, then you need to load the first "block" of size 1 "character" into memory. That is, only the length of the sequence of bytes changes, but the essence is the same.
Working with files is similar among different languages.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question