PHP file operation functions

Source: Internet
Author: User

1. Function file ('destination file ')
Read the file as an array and traverse the array in a loop to input
1. <?
2. header ('content-Type: text/html; charset = UTF-8 ');
3. $ file = file ("test.txt ");
4. if ($ file)
5 .{
6. foreach ($ file as $ num => $ content)
7 .{
8. echo "the number of rows is". $ num. "The content is". $ content. "<br> ";
9 .}
10 .}
11. else
12 .{
13. echo "File Reading failed ";
14 .}
15.?>
The output result is
 
2. Function fopen ('file to open ', 'Method used') to open a file, such as R + W + AB.
If W is used, the file will be automatically created if the file is not created.
1. <?
2. header ('content-Type: text/html; charset = UTF-8 ');
3. $ fopen = fopen ('t2. php', 'w ');
4. if ($ fopen)
5 .{
6. echo "file opened successfully ";
7 .}
8. else
9 .{
10. echo "file opening failed ";
11 .}
12.?>
Output result
The file is successfully opened and automatically created.
3. Function fwrite ('target file', 'written content') to write content to the target file'
1. <?
2. header ('content-Type: text/html; charset = UTF-8 ');
3. $ c2 = "One day I tried my best to take him to the market ";
4. $ fp = fopen ("t1.php", "AB ");
5.
6. if (fwrite ($ fp, $ c2 ))
7 .{
8. echo "Write successful ";
9 .}
10. else
11 .{
12. echo "Write failed ";
13 .}
14. if (fclose ($ fp ))
15 .{
16. echo "the document has been closed ";
17 .}
18.?>
The result is that the write is successful, and the file t1.php writes $ c2 content.
4. readfile ('destination file') reads the target file or website. Different from file, readfile directly reads the file content.
1. <?
2.
3. $ fp = readfile ("http://www.baidu.com ");
4. if (! $ Fp)
5 .{
6. echo "File Reading failed ";
7 .}
8.?>
Output result
 
5. filesize ('destination file') reads the size of the target file.
1. <?
2. $ filename = ("t1.php ");
3. $ size = filesize ($ filename );
4. echo $ size. "byts .";
5.?>

 
Output result, 186 byts
6. feof ('destination file') determines whether the pointer to the target file reaches the last line.
1. <? Php
2. header ('content-Type: text/html; charset = UTF-8 ');
3.
4. $ filename = "test.txt ";
5. if (file_exists ($ filename ))
6 .{
7. $ file = fopen ($ filename, "r ");
8.
9. // output all rows in the text until the end of the file.
10. while (! Feof ($ file ))
11 .{
12. echo fgets ($ file, 4096). "<br/> ";
13 .}
14.
15. fclose ($ file );
16 .}
17. else
18 .{
19. $ file = fopen ($ filename, "w ");
20. $ fw = fwrite ($ file, $ content );
21. if ($ fw)
22 .{
23. $ file = fopen ($ filename, "r ");
24. while (! Feof ($ file ))
25 .{
26. echo fgets ($ file). "<br> ";
27.
28 .}
29 .}
30. else
31 .{
32. echo "reading failed ";
33 .}
34. fclose ($ file );
35 .}
36.?>
Output content in test.txt
Use fget to traverse the array. When you open a file, you must use R to traverse the array with fget.
7. unlink ('destination file') deletes the target file
1. <?
2. header ('content-Type: text/html; charset = UTF-8 ');
3. $ filename = "t1.php ";
4. if (file_exists ($ filename ))
5 .{
6. unlink ($ filename );
7. echo $ filename. "The file is deleted successfully !! ";
8 .}
9. else
10 .{
11. echo "this file cannot be found ";
12 .}
13.?>
 
The output result is that the t1.php file is successfully deleted.
8. copy ('destination file', 'Copy location') can be copied to the current directory or to the specified exact location.
1. <?
2. header ('content-Type: text/html; charset = UTF-8 ');
3. $ filename = "test.txt ";
4. if (copy ($ filename, "d: \ test12.txt "))
5 .{
6. echo "file copied ";
7 .}
8. else
9 .{
10. echo "failed to copy the file ";
11 .}
12.?>
9
9. Create a folder by mkdir ('Folder name ')
1. <?
2. header ('content-Type: text/html; charset = UTF-8 ');
3.
4. function mk ($ dir)
5 .{
6.
7. if (file_exists ($ dir) & is_dir ($ dir ))
8 .{
9. echo "this folder name exists ";
10 .}
11. else
12 .{
13. if (mkdir ($ dir, 0777 ));
14. echo $ dir. "created successfully ";
15 .}
16 .}
17. mk (date ("Y-m-d "));
18.?>
Output result.: created successfully. In this example, a function is declared.
10. rmdir ('destination folder ') deletes a folder. Note that non-empty folders cannot be deleted. to delete a non-empty folder, traverse all the contents of the folder, delete the file, and then delete the folder.
Example
1. Use the realpath function to get the real address. If the address is null, equal to/, or the address is equal to: \, it indicates that it is the root directory and cannot be deleted. A false value is returned.
2. If the value is not equal to content in 1, use the opendir function to open the directory handle and return a directory stream.
Use while (readdir () to traverse the directory and assign the value to $ file.
3. If $ file is not equal to false, if it is equal to..., continue
4. Assign $ path $ dir (real address) connection to DIRECTORY_SEPARATOR (system separator) connection $ file. This address is the address of the file in the folder.
5. When $ path is a directory and rmdir ($ path) function is not false, it is when $ path is a directory but cannot be deleted.
Unlink ($ path); Delete this file. Close the handle. Delete the folder.
1. <?
2. header ('content-Type: text/html; charset = UTF-8 ');
3.
4. function mrdirs ($ dir)
5 .{
6.
7. $ dir = realpath ($ dir );
8. if ($ dir = ''| $ dir = '/' | (strlen ($ dir) = 3 & substr ($ dir, 1) = ':\\'))
9 .{
10. return false;
11 .}
12. else
13 .{
14. if (false! = ($ Dh = opendir ($ dir )))
15 .{
16. while (false! = ($ File = readdir ($ dh )))
17 .{
18. if ($ file = '.' | $ file = '..') {continue ;}
19. echo $ path = $ dir. DIRECTORY_SEPARATOR. $ file;
20. if (is_dir ($ path ))
21 .{
22. if (! Rmdir ($ path) {return false ;}}
23. else
24 .{
25. unlink ($ path );
26. echo $ path. "file to delete ";
27 .}
28 .}
29. closedir ($ dh );
30. rmdir ($ dir );
31. echo "folder deleted successfully ";
32. return true;
33 .}
34.
35. else
36 .{
37. return false;
38 .}
39.
40.
41 .}
42.
43.
44 .}
45.
46. $ dir = date ('Y-m-d ');
47. if (file_exists ($ dir ))
48 .{
49. mrdirs ($ dir );
50 .}
51. else
52 .{
53. echo "the folder does not exist and cannot be deleted ";
54 .}
55.?>
Output result. The folder has been deleted.
However, this method only applies to files in folders, and folder nesting does not work.
Author's "PHP Study Notes"

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.