PHP File Operation Basic code

Source: Internet
Author: User

PHP provides a series of I/O functions that are simple to implement the functions we need, including file system operations and directory operations (such as "copy"). The following brother even PHP training

Small series to introduce to you is the basic file read and write operations: (1) Read the file

;(2)

Write file (3) append to 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:

Read the file:

PHP Code:

1. <?php

2.

3. $file _name = "Data.dat";

4.//The absolute path of the file to be read: Homedata.dat

5.

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

7.//Open file, 8. "R" is a pattern, 9. Or the way we're going to do it, 10. See the introduction later in this article

11.

$file _read =fread ($file _pointer, FileSize ($file _name));

13.//through file refers to 14. Pin Read File contents

15.

Fclose ($file _pointer);

17.//Close File

18.

. print "The contents of the file read: $file _read";

20.//Display file contents

?>.

22.

Write file:

PHP Code:

1. <?php

2.

3. $file _name = "Data.dat";

4.//absolute path: Homedata.dat

5.

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

7.//"W" is a mode, 8. See later

9.

Fwrite ($file _pointer, "What Do You Wanna Write");

11.//Cut the file 12 first. is 0 bytes in size, 13. Then write

14.

Fclose ($file _pointer);

16.//End

17.

Print "Data successfully written to file";

19.

?>.

21st.

Append to file after:

PHP Code:

1. <?php

2.

3. $file _name = "Data.dat";

4.//absolute path: Homedata.dat

5.

6. $file _pointer =fopen ($file _name, "a");

7.//"W" mode

8.

9. Fwrite ($file _pointer, "What Do You Wanna Append");

10.//Not 11. Cut the file by 12. into 0 bytes, 13. Append data to the end of the file

14.

Fclose ($file _pointer);

16.//End

17.

Print "Data successfully appended to file";

19.

?>.

21st.

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 damage

Info.file

|1| mukul|15| male| India

|2| linus|31| male| Finland

|3| rob|27| male| usa|

bill|29| male| USA

In the example above, when PHP writes Rob's information to a file, Bill also starts writing, which is exactly the time to write the ' n ' of the Rob record, causing file corruption.

We certainly don't want this to happen, so let's take a look at the file lock:

PHP Code:

1. <?php

2.

3. $file _name = "Data.dat";

4.

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

6.

7. $lock =flock ($file _pointer, lock_sh);

8.//I use 4.0.2,9. So use lock_sh,10. You may need to write 1 directly.

11.

if ($lock) {

13.

$file _read =fread ($file _pointer, FileSize ($file _name));

$lock =flock ($file _pointer, Lock_un);

16.//If the version is less than php4.0.2,17. Replace Lock_un with 3

18.

19.}

20.

Fclose ($file _pointer);

22.

. print "File content is $file _read";

24.

?>.

26.

In the above example, if two files read.php and read2.php are to access the file, they can all be read, but when a program needs to write, it must wait until the read operation is complete and the file is freed.

PHP Code:

1. <?php

2.

3. $file _name = "Data.dat";

4.

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

6.

7. $lock =flock ($file _pointer, LOCK_EX);

8.//If the version is below php4.0.2,9. Replace LOCK_EX with 2

10.

One. if ($lock) {

12.

Fwrite ($file _pointer, "What U Wanna Write");

Flock ($file _pointer,lock_un);

15.//If the version is below php4.0.2,16. Replace Lock_un with 3

17.

18.}

19.

Fclose ($file _pointer);

21st.

Print "Data successfully written to file";

23.

?>.

25.

Although the "W" mode is used to overwrite the file, I don't think it applies.

PHP Code:

1. <?php

2.

3. $file _name = "Data.dat";

4.

5. $file _pointer =fopen ($file _name, "a");

6.

7. $lock =flock ($file _pointer, LOCK_EX);

8.//If the version is below php4.0.2,9. Replace LOCK_EX with 2

10.

One. if ($lock) {

12.

Fseek ($file _pointer, 0,seek_end);

14.//If the version is less than php4.0rc1,15. Using Fseek ($file _pointer, filsize ($file _name));

16.

Fwrite ($file _pointer, "What U Wanna Write");

Flock ($file _pointer,lock_un);

19.//If the version is below php4.0.2,20. Replace Lock_un with 3

21st.

22.}

23.

Fclose ($file _pointer);

25.

. Print "Data successfully written to file";

27.

?>.

29.

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

[b] [COLOR=0000FF] by the way, create the code for the file directory [/color][/b]

Create a similar ". /.. /.. Directory of/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 pathends 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);//mkdir function Create directory

}

}

}

Calls such as Createdirs ("xxx/xxxx/xxxx",);

Use $globals["Dirseparator" in the original function I changed it to '/'

Functionrecur_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 pathends in a dir not file

$subamount = 0;

}

else {

$subamount = 1;

}

These are just some basic information about the operation of the file code, I believe that beginners are very useful, posted here, I hope there is a point of the function!

PHP File Operation Basic code

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.