In the following example, we use the. txt file as an example to introduce the function for php to read local files. to read files, we can use fopen or file_get_contents to read them, file_get_contents is simpler, while fopen requires fread to display the read content. We use the. txt file as an example to introduce the function of php to read local files. we can use fopen or file_get_contents to read files, file_get_contents is simpler, and fopen requires fread to display the read content.
1. first introduce the fopen () function
Below we provide a code demo for directly opening a local file. we have added comments to all the necessary parts:
The code is as follows:
// Directly open the instance code of a local file
// Assume that your file is a text file named xmlas.txt.
$ Filedemo = "xmlas.txt ";
$ Fpdemo = fopen ($ filedemo, "r ");
If ($ fpdemo ){
While (! Feof ($ fpdemo )){
// 1000 characters read
$ Datademo = fread ($ fpdemo, 1000 );
}
Fclose ($ fpdemo );
}
Echo $ datademo;
?>
2. use the file_get_contents () function to output the entire file in a string:
Assume that the xmlas.txt text file has the following sentence: Nowadays, movies are getting less and less passionate. if you want to see all the climblies, please watch Japanese love.
Action movie!
The instance code of the file_get_contents () function is as follows:
The code is as follows:
Echo file_get_contents ("xmlas.txt ");
// The volume output content is in xmlas.txt:
// Today's movies are getting less and less passionate. if you want to see all the climax, please watch Japanese love action movies!
?>
3. how to read a local folder instead of a single file:
Please refer to the following instance code. in the instance, we will read a folder named xmlas:
The code is as follows:
$ Dirdemo = opendir ('/xmls ');
While ($ filedemo = readdir ($ dirdemo ))! = False ){
If ($ filedemo! = "." & $ Filedemo! = ".."){
$ Nsdemo = explode ('.', $ filedemo );
Echo $ nsdemo [0];
}
}
Closedir ($ dirdemo );
?>
4. we can also use fopen to write files.
The code is as follows:
/**
* Use fopen to write files
* @ Param string $ filename
* @ Param string $ contents
* @ Return boolean
*/
Function wirte ($ filename, & $ contents)
{
$ Fp = fopen ($ filename, "wb ");
If ($ fp)
{
Flock ($ fp, LOCK_EX); // lock the file at the same time, only one person can operate
Fwrite ($ fp, $ contents );
Flock ($ fp, LOCK_UN); // save the data grip to unlock the file and save it
Fclose ($ fp );
Return true;
} Else
{
Return false;
}
}
In this way, I can use fopen and fwrite to read and write files.
Note: l open the file
Before opening a file, we need to know the path of the file and whether the file exists.
Use the built-in global variables of $ _ SERVER ["DOCUMENT_ROOT"] to obtain the relative path of the site. As follows:
The code is as follows:
$ Root = $ _ SERVER ["DOCUMENT_ROOT"];
Use the file_exists () function to check whether a file exists. As follows:
The code is as follows:
If (! File_exists ("$ root/order.txt") {echo 'file does not exist ';}
This may be more reasonable and practical.