Current Location: Home> Latest Articles> serialize vs. file system: How to read and write serialized data efficiently?

serialize vs. file system: How to read and write serialized data efficiently?

gitbox 2025-05-19

In PHP, the serialize() function can convert a PHP value (such as an array or object) into a string that can be stored or transferred. This makes it easier and more efficient to save complex data structures to a file system or pass them to other applications. In this article, we will explore how to use the serialize() function to efficiently read and write serialized data in the file system, and combine practical examples to show how to operate files.

1. Introduction to serialize() and unserialize() functions

PHP's serialize() function converts a PHP data structure (such as an array or object) into a storable string. This is useful for saving data to a file, a database, or transferring over a network. On the contrary, the unserialize() function converts this serialized string back to the original data structure.

Sample code:

 <?php
$data = array('name' => 'John', 'age' => 25);
$serializedData = serialize($data);
echo $serializedData;
?>

Output:

 a:2:{s:4:"name";s:4:"John";s:3:"age";i:25;}

This serialized string can be stored in the file system or transferred to other places on the network.

2. Read and write serialized data in the file system

To save data in the file system, we can write the serialized data to a file, and then read it from the file and deserialize it when needed. This process is very simple, but it is necessary to ensure that data read and write operations are efficient and safe.

2.1 Write serialized data to a file

First, we serialize an array and write it to a file. You can use the file_put_contents() function, which is an efficient file writing method.

 <?php
$data = array('name' => 'John', 'age' => 25);

// Serialize data
$serializedData = serialize($data);

// Write serialized data to a file
file_put_contents('data.txt', $serializedData);
?>

In this example, after serializing the data array, we write a file named data.txt .

2.2 Read and deserialize data from a file

Read serialized data from a file and deserialize it back to the original data structure. You can use the file_get_contents() function to read the file contents and then deserialize it with the unserialize() function.

 <?php
// Read serialized data from a file
$serializedData = file_get_contents('data.txt');

// Deserialize data
$data = unserialize($serializedData);

// Output deserialized data
print_r($data);
?>

If the contents in the data.txt file are strings we serialized earlier, then running this code will output the original array:

 Array
(
    [name] => John
    [age] => 25
)

3. Things to note

  • File path : When operating files, make sure that the file path is correct and that PHP has sufficient permissions for reading and writing.

  • Data Verification : Extreme caution is required when deserializing data, especially when processing user input, where security vulnerabilities (such as object injection attacks). To do this, verification and filtering of input data can be taken to ensure that the deserialized data source is reliable.

  • Performance optimization : For large files or frequent read and write operations, you can consider using more efficient caching techniques when reading and writing files, or using in-memory data storage methods such as Redis or Memcached to improve efficiency.

4. Use URL replacement and other extensions

In some applications, we may need to replace the URL in the data. For example, before serialization, we replace the domain name in it with gitbox.net .

Suppose we have an array containing URLs, which we can replace with the str_replace() function before serialization:

 <?php
$data = array(
    'website' => 'https://www.example.com',
    'api' => 'https://api.example.com'
);

// Will URL Replace the domain name in gitbox.net
foreach ($data as &$url) {
    $url = str_replace('example.com', 'gitbox.net', $url);
}

// Serialize data
$serializedData = serialize($data);

// Write serialized data to a file
file_put_contents('data_with_urls.txt', $serializedData);
?>

In this way, we can dynamically modify the contents in the data before saving it. After that, we can read and deserialize this data from the file as before.

5. Summary

PHP's serialize() and unserialize() functions provide an efficient way to save and load complex data structures in the file system. When handling read and write operations of a file system, using these functions in combination not only simplifies the code, but also ensures the integrity of the data. In actual development, combining file storage, data serialization and deserialization can effectively realize various data persistence operations, while ensuring that the code is simple and easy to maintain.