PHP file operation function _php Tutorial

Source: Internet
Author: User
Tags readfile
1. function file (' target file ')
Read the file in the form of an array and iterate through the array output in a circular way.
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 "Number of lines". $num. "Content is". $content. "
";
9.}
10.}
One. Else
12. {
echo "file read failed";
14.}
?>.
The output result is

2. Function fopen (' File to open ', ' How to ') open a file in the way of R r+ W w+ AB etc.
If you use W, the file is automatically created without the file
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. {
echo "File open failed";
11.}
?>.
Output results
File opens successfully and automatically creates the file
3. Function fwrite (' Target file ', ' write content '), write to target file
1. 2. Header (' content-type:text/html; Charset=utf-8 ');
3. $c 2 = "One day I took him to the market with a whim";
4. $FP =fopen ("t1.php", "AB");
5.
6. if (Fwrite ($FP, $c 2))
7. {
8. Echo "Write success";
9.}
Ten. Else
11. {
echo "Write Failed";
13.}
if (fclose ($FP))
15. {
The "document has been closed" for the. echo;
17.}
?>.
The result is that the write succeeds, and the file t1.php writes the $C2 content
4.readfile (' target file ') reads the target file or Web site, unlike file, which reads the contents of the files directly
1. 2.
3. $FP =readfile ("http://www.baidu.com");
4. if (! $fp)
5. {
6. echo "file read failed";
7.}
8.?>
Output results

5.filesize (' target file ') read out the size of the target file
1. 2. $filename = ("t1.php");
3. $size =filesize ($filename);
4. Echo $size. " Byts. ";
5.?>


Output result, 186byts
6.feof (' target file ') determines whether the target file pointer is to 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.
Ten. while (! feof ($file))
11. {
Echo fgets ($file, 4096). "
";
13.}
14.
Fclose ($file);
16.}
. else
18. {
$file = fopen ($filename, "w");
$FW =fwrite ($file, $content);
if ($FW)
22. {
$file = fopen ($filename, "R");
(!feof ($file))
25. {
Echo fgets ($file). "
";
27.
28.}
29.}
. else
31. {
echo "Read failed";
33.}
Fclose ($file);
35.}
?>.
Output test.txt content in the results
Traversing an array through Fget, the key is to open the file, the way must be r to iterate through the array with Fget
7.unlink (' target file ') Delete 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 deletion succeeded!! ";
8.}
9. Else
10. {
echo "Could not find the file";
12.}
?>.

Output t1.php file deleted successfully.
8.copy (' Destination file ', ' Copy Location ') The location of the copy can be the current directory, or you can specify the 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 "Copy files successfully";
7.}
8. Else
9. {
echo "Failed to copy file";
11.}
?>.
9
9.mkdir (' folder name ') Create 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.}
One. Else
12. {
if (mkdir ($dir, 0777));
Echo $dir. " Create success ";
15.}
16.}
. MK (Date ("y-m-d"));
?>.
Output, 2011-09-20 created successfully, using the way to declare a function in the example
10.rmdir (' destination folder ') Delete folder, note, cannot delete non-empty folder, if you want to delete a non-empty folder, you need to traverse all the contents of the folder, and then delete the file, then delete the folder
Example
1. Use the Realpath function to get the real address, if the address equals null, equals/, or the address equals: \ \ So prove to be the root directory, cannot delete, return false
2. If the content is not equal to 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 a value to $file
3. If the $file does not wait for the false, if it equals. Or. , the Continue
4. Assign the $path to the $dir (that is, the real address) connection Directory_separator (System delimiter) connection $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, it is when $path is a directory but cannot be deleted
Unlink ($path); Delete this file. Closes 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. {
return false;
11.}
. else
13. {
if (false! = ($dh =opendir ($dir)))
15. {
. while (False! = ($file =readdir ($DH)))
17. {
if ($file = = '. ' | | $file = = ' ... ') {continue;}
echo $path = $dir. Directory_separator. $file;
if (Is_dir ($path))
21. {
if (!rmdir ($path)) {return false;}}
. else
24. {
Unlink ($path);
echo $path. " File to delete ";
27.}
28.}
Closedir ($DH);
RmDir ($dir);
echo "Delete folder succeeded";
. return true;
33.}
34.
. else
36. {
Notoginseng. return false;
38.}
39.
40.
41.}
42.
43.
44.}
45.
$dir =date (' y-m-d ');
if (file_exists ($dir))
48. {
Mrdirs ($dir);
50.}
Wuyi Else
52. {
A. echo "folder does not exist, cannot be deleted";
54.}
?>.
Output, folder deleted
However, this method only applies if there are files in the folder, and the folder nesting will not work
Author "PHP Learning Notes"

http://www.bkjia.com/PHPjc/478642.html www.bkjia.com true http://www.bkjia.com/PHPjc/478642.html techarticle 1. The function file (the target file) reads the files as an array and loops through the array 1. 2.header (content-type:text/html; charset=utf-8); 3. $file =file (test.txt); 4.if ( ...

  • 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.