PHP dat file read and write operation example _php tutorial

Source: Internet
Author: User
Tags flock fread
In PHP no matter what type of file to read is mostly using the fopen function, and then with the other functions of the operation, let me introduce the method of reading DAT data file.

The following is an article on basic file read and write operations, I used to read this article after learning the basic operation of the file, sent here to share with you: Copy the content to the Clipboard

The code is as follows Copy Code

$file _name = "Data.dat";
Absolute path of the file to read: Homedata.dat

$file _pointer = fopen ($file _name, "R");
Open the file, "R" is a pattern, or the way we do it, as described later in this article

$file _read = fread ($file _pointer, FileSize ($file _name));
Read file contents via file pointer

Fclose ($file _pointer);
Close File

Print "The contents of the file read: $file _read";
Show file contents
?>

Code:

The code is as follows Copy Code

$file _name = "Data.dat";
Absolute path: Homedata.dat

$file _pointer = fopen ($file _name, "w");
"W" is a pattern, see later

Fwrite ($file _pointer, "What Do You Wanna Write");
First cut the file to a size of 0 bytes, then write

Fclose ($file _pointer);
End

Print "Data successfully written to file";

?>

Code:

The code is as follows Copy Code

$file _name = "Data.dat";
Absolute path: Homedata.dat

$file _pointer = fopen ($file _name, "a");
"W" mode

Fwrite ($file _pointer, "What Do You Wanna Append");
Do not cut the file into 0 bytes, append the data to the end of the file

Fclose ($file _pointer);
End

Print "Data successfully appended to file";

?>

The above is just a brief introduction, below we will discuss some deeper.

Sometimes a multi-person write scenario (most often on a Web site with a large traffic) produces useless data written to the file, such as:

Info.file file contents are as follows

-

|1| mukul|15| male| India (N)
|2| linus|31| male| Finland (N) Now two people registered at the same time, causing file corruption

-

Info.file

|1| mukul|15| male| India
|2| linus|31| male| Finland
|3| rob|27| male| usa|
bill|29| male| In the USA example, when PHP writes Rob's information to a file, Bill also begins writing, which is exactly what it takes to write Rob's record of ' n ', causing file corruption.

We certainly don't want this to happen, so let's take a look at the file Lock: copy content to clipboard
Code:

The code is as follows Copy Code

$file _name = "Data.dat";

$file _pointer = fopen ($file _name, "R");

$lock = Flock ($file _pointer, lock_sh);
I use 4.0.2, so with lock_sh, you may need to write directly to 1.

if ($lock) {

$file _read = fread ($file _pointer, FileSize ($file _name));
$lock = Flock ($file _pointer, Lock_un);
If the version is less than PHP4.0.2, replace the Lock_un with 3

}

Fclose ($file _pointer);

Print "File content is $file _read";

?> The above example, if two files read.php and read2.php all want to access the file, then they can be read, but when a program needs to write, it must wait until the read operation is complete, the file is freed. Copy Content to Clipboard
Code:

The code is as follows Copy Code

$file _name = "Data.dat";

$file _pointer = fopen ($file _name, "w");

$lock = Flock ($file _pointer, LOCK_EX);
If the version is lower than PHP4.0.2, replace the LOCK_EX with 2

if ($lock) {

Fwrite ($file _pointer, "What U Wanna Write");
Flock ($file _pointer, Lock_un);
If the version is lower than PHP4.0.2, replace the Lock_un with 3

}

Fclose ($file _pointer);

Print "Data successfully written to file";

?>

Hmmm., for append data is a little different from other operations, that is fseek! It is always a good practice to confirm that the file pointer is at the end.

If it is under Windows system, the file name above must be preceded by a ".

Flock:

Flock () is locked only when the file is open. The file is locked after it is opened, and now the contents of the file are only in the content at that time and do not reflect the results of other program operations, so it is not only the file append operation, but also the read operation should use Fseek.
(the translation may not be very precise here, but I think it's a good idea.)

About Patterns:

' R '-read-only open, file pointer placed on file header

' r+ '-read and write mode open, the file pointer is placed on the file header

' W '-only write open, the file pointer is placed on the file header, the file is clipped to 0 bytes, if the file does not exist, try to build the file

' w+ '-read and write open, the file pointer is placed on the file header, the file size is clipped to 0 bytes, if the file does not exist, try to build the file

' A '-write-only open, the file pointer is at the end of the file, if the file does not exist, try to create a file

' A + '-read and write open, file pointer at the end of the file, if the file does not exist, try to create a file

http://www.bkjia.com/PHPjc/629904.html www.bkjia.com true http://www.bkjia.com/PHPjc/629904.html techarticle in PHP no matter what type of file to read is mostly using the fopen function, and then with the other functions of the operation, let me introduce the method of reading DAT data file. Here is an article ...

  • 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.