PHP file read/write operations

Source: Internet
Author: User
Tags flock

PHP file read/write operations


PHP provides a series of I/O functions to implement the functions we need, including file system operations and directory operations (such as "copy [copy]"). The following describes basic file read/write operations: (1) reading files; (2) Writing files; (3) appending to files.

The following is an article about basic file read/write operations. I have read this article and learned about basic file operations. I will share it with you here:
Read files:
PHP code:
1. <? Php
2. & nbsp;
3. $ file_name = "data. dat ";
4. // the absolute path of the file to be read: homedata. dat
5. & nbsp;
6. $ file_pointer = fopen ($ file_name, "r ");
7. // open the file. 8. "r" is a mode, 9. Or the operation method we want to perform. 10. For details, refer to the introduction below.
11. & nbsp;
12. $ file_read = fread ($ file_pointer, filesize ($ file_name ));
13. // read the file content by referring to 14.
15. & nbsp;
16. fclose ($ file_pointer );
17. // close the file
18. & nbsp;
19. print "the file content read is: $ file_read ";
20. // display the File Content
21.?>
22. & nbsp;

Write File:
PHP code:
1. <? Php
2. & nbsp;
3. $ file_name = "data. dat ";
4. // absolute path: homedata. dat
5. & nbsp;
6. $ file_pointer = fopen ($ file_name, "w ");
7. // "w" is a mode. 8. For details, refer to the following section.
9. & nbsp;
10. fwrite ($ file_pointer, "what you wanna write ");
11. // cut the file 12 first. It is 0 bytes, 13. Then write
14. & nbsp;
15. fclose ($ file_pointer );
16. // end
17. & nbsp;
18. print "data is successfully written to the file ";
19. & nbsp;
20.?>
21. & nbsp;

Append to the end of the file:
PHP code:
1. <? Php
2. & nbsp;
3. $ file_name = "data. dat ";
4. // absolute path: homedata. dat
5. & nbsp;
6. $ file_pointer = fopen ($ file_name, "");
7. // "w" Mode
8. & nbsp;
9. fwrite ($ file_pointer, "what you wanna append ");
10. // not 11. Cut the file 12. into 0 bytes, 13. append the data to the end of the file
14. & nbsp;
15. fclose ($ file_pointer );
16. // end
17. & nbsp;
18. print "data is successfully appended to the file ";
19. & nbsp;
20.?>
21. & nbsp;

The above is just a brief introduction. Next we will discuss some deeper ones.

Sometimes many people write data (most often on websites with large traffic), and useless data is generated to write files. For example:

The content of the info. file is as follows->

| 1 | Mukul | 15 | Male | India (n)
| 2 | Linus | 31 | Male | Finland (n)

Now two people register at the same time, causing file destruction->

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 begins writing. At this time, it is necessary to write 'n' of Rob's record, causing file damage.

We certainly don't want this to happen, so let's look at the file lock:
PHP code:
1. <? Php
2. & nbsp;
3. $ file_name = "data. dat ";
4. & nbsp;
5. $ file_pointer = fopen ($ file_name, "r ");
6. & nbsp;
7. $ lock = flock ($ file_pointer, LOCK_SH );
8. // I use 4.0.2, 9. So I use LOCK_SH, 10. You may need to write 1 directly.
11. & nbsp;
12. if ($ lock ){
13. & nbsp;
14. $ file_read = fread ($ file_pointer, filesize ($ file_name ));
15. $ lock = flock ($ file_pointer, LOCK_UN );
16. // if the version is earlier than PHP4.0.2, 17. Use 3 to replace LOCK_UN
18. & nbsp;
19 .}
20. & nbsp;
21. fclose ($ file_pointer );
22. & nbsp;
23. print "File Content: $ file_read ";
24. & nbsp;
25.?>
26. & nbsp;

In the preceding example, if two files are read. php and read2.php both need to access the file, so they can be read, but when a program needs to write, it must wait until the read operation is complete and the file is released.
PHP code:
1. <? Php
2. & nbsp;
3. $ file_name = "data. dat ";
4. & nbsp;
5. $ file_pointer = fopen ($ file_name, "w ");
6. & nbsp;
7. $ lock = flock ($ file_pointer, LOCK_EX );
8. // if the version is earlier than PHP4.0.2, 9. Replace LOCK_EX with 2.
10. & nbsp;
11. if ($ lock ){
12. & nbsp;
13. fwrite ($ file_pointer, "what u wanna write ");
14. flock ($ file_pointer, LOCK_UN );
15. // if the version is earlier than PHP4.0.2, 16. Use 3 to replace LOCK_UN
17. & nbsp;
18 .}
19. & nbsp;
20. fclose ($ file_pointer );
21. & nbsp;
22. print "data is successfully written to the file ";
23. & nbsp;
24.?>
25. & nbsp;

Although the "w" mode is used to overwrite files, I don't think it is applicable.
PHP code:
1. <? Php
2. & nbsp;
3. $ file_name = "data. dat ";
4. & nbsp;
5. $ file_pointer = fopen ($ file_name, "");
6. & nbsp;
7. $ lock = flock ($ file_pointer, LOCK_EX );
8. // if the version is earlier than PHP4.0.2, 9. Replace LOCK_EX with 2.
10. & nbsp;
11. if ($ lock ){
12. & nbsp;
13. fseek ($ file_pointer, 0, SEEK_END );
14. // if the version is earlier than PHP4.0RC1, 15. Use fseek ($ file_pointer, filsize ($ file_name ));
16. & nbsp;
17. fwrite ($ file_pointer, "what u wanna write ");
18. flock ($ file_pointer, LOCK_UN );
19. // if the version is earlier than PHP4.0.2, 20. Use 3 to replace LOCK_UN
21. & nbsp;
22 .}
23. & nbsp;
24. fclose ($ file_pointer );
25. & nbsp;
26. print "data is successfully written to the file ";
27. & nbsp;
28.?>
29. & nbsp;

Hmmm... the append data is a bit different from other operations, that is, FSEEK! It is always a good habit to make sure that the file pointer is at the end of the file.

In Windows, ''must be added before the file name ''.

FLOCK Miscellaneous:

Flock () is locked only after the file is opened. In the above column, the file is locked only after it is opened. The content of the file is only in the current time, but does not reflect the results of other program operations. Therefore, it is not just an append operation on the file, fseek should also be used for read operations.
(The translation here may not be very accurate, but I have figured it out ).

Mode:

'R'-open in read-only mode, and place the file pointer in the file header

'R + '-open in read/write mode, and place the file pointer in the file header

'W'-Write-only open, the file pointer is placed in the file header, the file is cut to 0 bytes, if the file does not exist, try to create a file

'W + '-open read/write. The file pointer is placed in the file header. The file size is cut to 0 bytes. If the file does not exist, try to create a 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 a file.

'A + '-open read/write. The file pointer is placed at the end of the file. If the file does not exist, try to create the file.

By the way, the code for creating a file directory

// Create a directory similar to "../xxx/xxx.txt"

Function createdirs ($ path, $ mode = 0777) // mode 077
{
$ Dirs = explode ('/', $ path );
$ Pos = strrpos ($ path ,".");
If ($ pos = false) {// note: three equal signs
// Not found, means path ends in a dir not file
$ Subamount = 0;
}
Else {
$ Subamount = 1;
}

For ($ c = 0; $ c <count ($ dirs)-$ subamount; $ c ++ ){
$ Thispath = "";
For ($ cc = 0; $ cc <= $ c; $ cc ++ ){
$ Thispath. = $ dirs [$ cc]. '/';
}
If (! File_exists ($ thispath )){
// Print "$ thispath
";
Mkdir ($ thispath, $ mode); // create a directory using the mkdir Function
}
}
}
// Call such as createdirs ("xxx/xxxx ",);

// In the original function, I changed $ GLOBALS ["dirseparator"] '/'

Function recur_mkdirs ($ path, $ mode = 0777) // mode 0777
{
// $ GLOBALS ["dirseparator"]
$ Dirs = explode ($ GLOBALS ["dirseparator"], $ path );
$ Pos = strrpos ($ path ,".");
If ($ pos = false) {// note: three equal signs
// Not found, means path ends in a dir not file
$ Subamount = 0;
}
Else {
$ Subamount = 1;
}

These are just some basic code for file operations. I believe they are useful to beginners. I hope you can introduce them here!

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.