Home >Backend Development >PHP Tutorial >PHP steps to replace template variables_PHP tutorial
1. First you need to open a file. PHP is used here ->fopen(); function
Definition and usage
fopen() function opens a file or URL.
If the opening fails, this function returns FALSE.
Function prototype:
fopen(filename,mode,include_path,context)
http://www.jb51.net/w3school/php/func_filesystem_fopen.asp.htm
Description
fopen( ) Binds the name resource specified by filename to a stream. If filename is of the form "scheme://...", it is treated as a URL and PHP will search for a protocol handler (also called a wrapper protocol) to handle the scheme. If the wrapper protocol has not been registered for the protocol, PHP will emit a message to help check for potential problems in the script and continue execution of filename as if it were a normal filename.
If PHP thinks filename specifies a local file, it will try to open a stream on that file. The file must be accessible to PHP, so you need to confirm that the file access permissions allow this access. If safe mode or open_basedir is activated further restrictions apply.
If PHP believes that filename specifies a registered protocol, and the protocol is registered as a network URL, PHP will check and confirm that allow_url_fopen has been activated. If it is closed, PHP will issue a warning and the call to fopen will fail.
Support for context was added in PHP 5.0.0.
Tips and Notes
Note: For portability reasons, it is strongly recommended to always use the "b" flag when opening files with fopen().
2. After opening this file, read the file. PHP ->fread(); function is used here
Definition and usage
fread() function reading file (safe for use with binary files).
Function prototype:
fread(file,length) //Note: I just know. The file obtained by this function calculates the file size in bytes....
http: //www.jb51.net/w3school/php/func_filesystem_fread.asp.htm
Description
fread() reads up to length bytes from the file pointer file. This function is called after reading up to length bytes, or when EOF is reached, or (for network streams) when a packet is available, or (after opening a user space stream) 8192 bytes have been read. Will stop reading the file, depending on which condition is encountered first.
Returns the string read, or returns false if an error occurs.
Tips and Notes
Tip: If you just want to read the contents of a file into a string, please use file_get_contents(), which has much better performance than fread().
Example 1
Read 10 bytes from the file: