PHP file operation functions_PHP tutorial

WBOY
Release: 2016-07-13 17:45:58
Original
742 people have browsed it

1. Function file('target file')
Read the file in the form of an array, and use a loop to traverse the array and 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 lines is ".$num." and the content is ".$content."
";
9. }  
10. }
11. else
12. {
13. echo "File reading failed";
14. }
15. ?>
The output result is

2. Function fopen ('file to open', 'in which method') opens a file, the methods include R R+ W W+ AB, etc.
If W is used, the file will be automatically created if it does not exist
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 results
The file is opened successfully and the file is automatically created
3. Function fwrite('target file', 'written content'), writes content to the target file
1. 2. header('Content-Type:text/html; charset=utf-8');
3. $c2="One day I took him to the market on a whim";
4. $fp=fopen("t1.php","ab");
5.
6. if (fwrite($fp,$c2))
7. {
8. echo "Write successfully";
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 writing is successful and the file t1.php writes the content of $c2
4.readfile('target file') reads the target file or website. Different from file, it directly reads the content of the file
1. 2.
3. $fp=readfile("http://www.baidu.com");
4. if (!$fp)
5. {
6. echo "File reading failed";
7. }
8. ?>
Output results

5.filesize('target 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('target file') determines whether the target file pointer reaches the last line
1. 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 lines in the text until the end of the file.
10. while(! feof($file))
11. {
12. echo fgets($file,4096). "
";
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)."
";
27.       
28. }
29. }
30. else
31.                       { 
32. echo "Read failed";
33. }
34. fclose($file);
35. }
36. ?>
Output the content in test.txt
Traverse the array through fget. Key points: When opening a file, the mode must be R before you can use fget to traverse the array
7.unlink('target 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."File deleted successfully!!";
8. }
9. else
10. {
11. echo "The file cannot be found";
12. }
13. ?>

Output result: The t1.php file is deleted successfully.
8.copy('target file','copied location') The copied location can be the current directory, or you can specify the precise 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 successfully";
7. }
8. else
9. {
10. echo "Failed to copy file";
11. }
12. ?>
9
9.mkdir('folder name') creates a folder
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 "The 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, 2011-09-20 Creation successful, the example uses the method of declaring a function
10.rmdir('target folder') deletes folders. Note that non-empty folders cannot be deleted. If you want to delete non-empty folders, you need to traverse all the contents in the folder, then delete the files first, and then delete the folder
Example
1. Use the realpath function to get the real address. If the address is equal to empty, equal to /, or the address is equal to:\, then it proves to be the root directory and cannot be deleted, and returns false
2. If it is not equal to the content in 1, then use the opendir function to open the directory handle and return a directory stream
Use while (readdir()) to traverse the directory and assign it to $file
3. If $file is not equal to false, if it is equal to. or.., continue
4. Assign $path to $dir (which is the real address) and connect to DIRECTORY_SEPARATOR (system separator) to $file. This address is the address of the file in the folder
5. When $path is a directory and the rmdir($path) function is not false, that is when $path is a directory but cannot be deleted
unlink($path); delete this file. Close the handle. Delete the folder again.
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);
" 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 results, the folder has been deleted
But this method only works if there are files in the folder. Nesting of folders will not work
Author "PHP Study Notes"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478642.htmlTechArticle1. Function file (target file) reads the file in the form of an array, loops through the array and inputs 1 .? 2.header(Content-Type:text/html; charset=utf-8); 3.$file=file(test.txt); 4.if(...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!