To divide a string according to the stored content:
File_put_contents: writing a string to a file
Syntax: int file_put_contents ( string $filename
, mixed $data
[, int $flags
= 0 [, resource $context
]] )
Parameters
filename:
The name of the file to be written to.
data:
The data to write. The type can be a string, anarray, or a stream resource (as mentioned above).
If data
specified as a stream resource, the cached data stored in stream here is written to the specified file, which is similar to using the Stream_copy_to_stream () function.
The argument data
can be an array (but not a multidimensional array), which is equivalent to file_put_contents ($filename, Join (", $array)).
flags:
flags
The value can be a combination of the following flag using the OR (|) operator.
Available Flags
Flag |
Description |
FILE_USE_INCLUDE_PATH |
Search in the Include directory filename . More information can be found in include_path. |
FILE_APPEND |
filename Append data instead of overwrite if the file already exists. |
LOCK_EX |
An exclusive lock is obtained when writing. |
context:
A context resource.
//No flag used
<?PHP$file= ' People.txt ';//Open the file to get existing content$current=file_get_contents($file);//Append A new person to the file$current. = "John smith\n";//Write the contents back to the filefile_put_contents($file,$current);?>
//Use flag<?PHP$file= ' People.txt ';//The new person to add to the file$person= "John smith\n";//Write the contents to the file,//using the FILE_APPEND flag to APPEND the content to the end of the file//and the LOCK_EX flag to prevent anyone else writing to the file at the same timefile_put_contents($file,$person, File_append |lock_ex);?>
Non-string serialization: A representation of a stored value (string) is generated. That is, the object or array, etc., is converted to a type (string) that can be stored in the file
Syntax:string serialize ( mixed $value
)
Serialize () returns a string that contains the value
byte stream represented and can be stored anywhere.
This facilitates the storage or delivery of PHP values without losing their type and structure.
To change the serialized string back to PHP's value, use unserialize (). serialize () can handle any type except resource. You can even serialize () those arrays that contain pointers to their own references. The references in the array/object you are serialize () will also be stored.
When serializing an object, PHP will attempt to call the object's member function __sleep () before the sequence action . This allows the object to do any cleanup before it is serialized. Similarly, when you use unserialize () to restore an object, the __wakeup () member function is called.
Serialization:
<?PHP//$session _data is a multidimensional array containing the current user session information. We use serialize () to store it in the database before the request ends. $conn=Odbc_connect("WebDB", "PHP", "chicken");$stmt=Odbc_prepare($conn, "UPDATE sessions SET data =?" WHERE id =? ");$sqldata=Array(Serialize($session _data),$PHP _auth_user);if(!Odbc_execute($stmt, &$sqldata)) { $stmt=Odbc_prepare($conn, "INSERT into sessions (ID, data) VALUES (?,?)"); if(!Odbc_execute($stmt, &$sqldata)) { /*Error*/ }}?>
Deserialization:Unserialize () operates on a single serialized variable (string) and converts it back to the value of PHP.
Syntax:mixed unserialize ( string $str
)
str:
The serialized string.
If the deserialized variable is an object, after successfully re-constructing the object, PHP will automatically attempt to invoke the __wakeup () member function (if one exists).
NOTE:UNSERIALIZE_CALLBACK_FUNC directive
If you need to instantiate an undefined class at the time of the deserialization, you can set the callback function for the call (lest you get an incomplete object "__php_incomplete_class"). Can pass php.ini ,ini_set () or .htaccess define ' Unserialize_callback_func '. It is called every time a undefined class is instantiated. To disable this feature, simply empty the setting.
<?PHP//here, we use Unserialize () to load session data from the $session _data array from the database. This example is a supplement to the example that describes serialize (). $conn=Odbc_connect("WebDB", "PHP", "chicken");$stmt=Odbc_prepare($conn, "Select data from sessions WHERE ID =?");$sqldata=Array($_server[' Php_auth_user ']);if(!Odbc_execute($stmt,$sqldata) || !Odbc_fetch_into($stmt,$tmp)) { //initialize to an empty array if an error occurs or is returned $session _data=Array();} Else { //What we need now is the serialized data in the $tmp [0]. $session _data=unserialize($tmp[0]); if(!Is_array($session _data)) { //error, initializing to an empty array $session _data=Array(); }}?>
Unserialize_callback_func Example
<? PHP $serialized _object= ' o:1: "A": 1:{s:5: "Value"; s:3: "100";} ' ; // Unserialize_callback_func available from PHP 4.2.0 Ini_set // Set your callback function function mycallback ($classname) { // simply include the file containing the class definition //$ ClassName indicates which class is required }?>
Other
Unserialize, the corresponding string is constructed, in turn, into the original data type
Why not an object of the person class?
Because you want to get the object of a class, you must ensure that the current class has already been loaded.
Workaround: Load the corresponding class in advance.
Deserialization Auto-load
PHP--use text to store content, file_put_contents,serialize,unserialize