Common functions for php to read local files (fopen and file_get_contents)

Source: Internet
Author: User
Tags flock

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 can display the read content only when fread is used.

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:

Copy codeThe Code is as follows:
// Directly open the instance code of a local file
<? Php
// 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:

Copy codeThe Code is as follows:
<? Php
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:

Copy codeThe Code is as follows:
<? Php
$ 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.

Copy codeThe 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:

Copy codeThe Code is as follows: $ root = $ _ SERVER ["DOCUMENT_ROOT"];

Use the file_exists () function to check whether a file exists. As follows:

Copy codeThe Code is as follows:
If (! File_exists ("$ root/order.txt") {echo 'file does not exist ';}

This may be more reasonable and practical.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.