Current Location: Home> Latest Articles> How to use serialize to store PHP data as a text file?

How to use serialize to store PHP data as a text file?

gitbox 2025-05-19

In PHP, the serialize() function can convert a PHP variable (such as arrays, objects, etc.) into a string that can be stored or transferred. This function is often used to save data to files or transfer it over the network. In this article, we will discuss how to use the serialize() function to store data as a text file and introduce specific operation steps.

Step 1: Prepare the data

First, we need to prepare some data, which can usually be an array, object, or other PHP type that can be serialized. Here we take arrays as an example:

 $data = array(
    'name' => 'John Doe',
    'email' => '[email protected]',
    'age' => 28,
    'skills' => array('PHP', 'JavaScript', 'HTML')
);

Step 2: Use serialize() function to convert data into string

Use the serialize() function to convert the array $data to a string:

 $serializedData = serialize($data);

Now, the $serializedData variable is stored in a string that can be easily stored into a file.

Step 3: Write serialized data to text file

Next, we save the serialized data to a text file. You can use PHP's file_put_contents() function to write strings to a file:

 $file = 'data.txt';  // file name
file_put_contents($file, $serializedData);

In this way, the serialized data is stored in a file named data.txt .

Step 4: Read data from the file and deserialize it

When you need to read this file and restore the original data structure, you can use the file_get_contents() function to read the file contents, and then use the unserialize() function to convert the serialized string back to the PHP data structure.

 $storedData = file_get_contents($file);
$unserializedData = unserialize($storedData);

At this point, $unserializedData will contain the original array structure.

Sample code

 <?php
// Data preparation
$data = array(
    'name' => 'John Doe',
    'email' => '[email protected]',
    'age' => 28,
    'skills' => array('PHP', 'JavaScript', 'HTML')
);

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

// Save serialized data to a text file
$file = 'data.txt';
file_put_contents($file, $serializedData);

// Read data from a file and deserialize it
$storedData = file_get_contents($file);
$unserializedData = unserialize($storedData);

// Print deserialized data
print_r($unserializedData);
?>

Things to note

  1. File permissions : Make sure you have permission to write data to the target file.

  2. Data security : Be careful when deserializing content provided by users to avoid potential security risks (such as object injection attacks). You can usually use the second parameter of the unserialize() function to limit the class type during deserialization, or use other safe methods.

  3. Performance considerations : Serialization and deserialization operations may take up more resources for very large data sets. Storage and reading mechanisms can be optimized as needed.

Summarize

By using PHP's serialize() and unserialize() functions, you can easily store data as a text file and recover data when needed. This method is often used in scenarios such as caching, session management, or cross-platform data transmission. As long as you master these basic operations, you can effectively use PHP to handle data storage and recovery tasks.