Home > Backend Development > PHP Tutorial > Detailed explanation of dat file reading and writing operations in PHP_PHP tutorial

Detailed explanation of dat file reading and writing operations in PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:07:33
Original
1653 people have browsed it

In PHP, no matter what type of file is read, most of the time it is to use the fopen function, and then cooperate with other functions to operate. Next, I will introduce the method of reading dat data files.

The following is an article about basic file reading and writing operations. I once learned basic file operations after reading this article. I post it here to share with everyone: Copy content to the clipboard

The code is as follows Copy code
 代码如下 复制代码

$file_name = "data.dat";
// 要读取的文件的绝对路径: homedata.dat

$file_pointer = fopen($file_name, "r");
// 打开文件,"r" 是一种模式,或者说我们要进行的操作方法,详见本文后面的介绍

$file_read = fread($file_pointer, filesize($file_name));
// 通过文件指针读取文件内容

fclose($file_pointer);
// 关闭文件

print "读取到的文件内容是: $file_read";
// 显示文件内容
?>

$file_name = "data.dat";

// Absolute path of the file to be read: homedata.dat

$file_pointer = fopen($file_name, "r");

// Open the file, "r" is a mode, or the operation method we want to perform, please see the introduction later in this article
 代码如下 复制代码

$file_name = "data.dat";
// 绝对路径: homedata.dat

$file_pointer = fopen($file_name, "w");
// "w"是一种模式,详见后面

fwrite($file_pointer, "what you wanna write");
// 先把文件剪切为0字节大小, 然后写入

fclose($file_pointer);
// 结束

print "数据成功写入文件";

?>

$file_read = fread($file_pointer, filesize($file_name));

// Read the file content through the file pointer

fclose($file_pointer);
 代码如下 复制代码

$file_name = "data.dat";
// 绝对路径: homedata.dat

$file_pointer = fopen($file_name, "a");
// "w"模式

fwrite($file_pointer, "what you wanna append");
// 不把文件剪切成0字节, 把数据追加到文件最后

fclose($file_pointer);
// 结束

print "数据成功追加到文件";

?>

// Close file

print "The content of the file read is: $file_read";

//Display file content

?>

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 mode, see below for details

fwrite($file_pointer, "what you wanna write");
// First cut the file to 0 bytes, then write

fclose($file_pointer);

// End


print "Data written to file successfully";

 代码如下 复制代码

 

$file_name = "data.dat";

$file_pointer = fopen($file_name, "r");

$lock = flock($file_pointer, LOCK_SH);
// 我使用4.0.2,所以用LOCK_SH,你可能需要直接写成 1.

if ($lock) {

$file_read = fread($file_pointer, filesize($file_name));
$lock = flock($file_pointer, LOCK_UN);
// 如果版本小于PHP4.0.2, 用 3 代替 LOCK_UN

}

fclose($file_pointer);

?>
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 you wanna append"); // Do not cut the file to 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, let’s discuss some deeper ones below. Sometimes multiple people write (most commonly on websites with large traffic), which results in useless data being written to the file, for example: The content of the info.file file is as follows -> |1|Mukul|15|Male|India (n) |2|Linus|31|Male|Finland (n) Now two people are registered at the same time, causing file damage -> info.file -> |1|Mukul|15|Male|India |2|Linus|31|Male|Finland |3|Rob|27|Male|USA| Bill|29|Male|USA In the above example, when PHP writes Rob's information to the file, Bill also starts writing. At this time, it happens that the 'n' of Rob's record needs to be written, causing the file to be damaged. We certainly don’t want this to happen, so let’s look at file locking: 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 to use LOCK_SH, you may need to write it directly as 1. if ($lock) { $file_read = fread($file_pointer, filesize($file_name)); $lock = flock($file_pointer, LOCK_UN); // If the version is smaller than PHP4.0.2, use 3 instead of LOCK_UN } fclose($file_pointer);

print "The file content is $file_read";

?>In the above example, if both files read.php and read2.php have to access the file, then they can both read, but when a program needs to write, it must wait until The read operation is completed and the file is released. Copy content to clipboard
Code:

?>
The code is as follows
 代码如下 复制代码

 

$file_name = "data.dat";

$file_pointer = fopen($file_name, "w");

$lock = flock($file_pointer, LOCK_EX);
// 如果版本低于PHP4.0.2, 用 2 代替 LOCK_EX

if ($lock) {

fwrite($file_pointer, "what u wanna write");
flock($file_pointer, LOCK_UN);
// 如果版本低于PHP4.0.2, 用 3 代替 LOCK_UN

}

fclose($file_pointer);

print "数据成功写入文件";

?>

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, use 2 instead of LOCK_EX

if ($lock) {

fwrite($file_pointer, "what u wanna write");

flock($file_pointer, LOCK_UN);

// If the version is lower than PHP4.0.2, use 3 instead of LOCK_UN

}

fclose($file_pointer);

print "Data written to file successfully";

Hmmm..., Appending data is a little different from other operations, that is FSEEK! It is always a good practice to make sure the file pointer is at the end of the file. If it is under Windows system, the above file needs to be preceded by ''. FLOCK Talk: Flock() only locks the file after it is opened. In the above column, the file is locked after it is opened. Now the content of the file is only the content at that time, and does not reflect the results of other program operations. Therefore, fseek should be used not only for file append operations, but also for read operations. (The translation here may not be exact, but I think I get the idea). About the mode: 'r' - Open in read-only mode, the file pointer is placed at the beginning of the file
'r+' - Open in read-write mode, place the file pointer at the file header 'w' - open for writing only, the file pointer is placed at the beginning of the file, the file is cut to 0 bytes, if the file does not exist, try to create the file
'w+' - Open for reading and writing, the file pointer is placed at the file header, the file size is cut to 0 bytes, if the file does not exist, try to create the file 'a' - Open in write-only mode, the file pointer is placed at the end of the file, if the file does not exist, try to create the file 'a+' - open for reading and writing, the file pointer is placed at the end of the file, if the file does not exist, try to create the file http://www.bkjia.com/PHPjc/629904.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629904.htmlTechArticleIn PHP, no matter what type of file is read, most of the time it is to use the fopen function, and then cooperate with other functions to operate , let me introduce the method of reading dat data file. The following is an article...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template