Other commonly used File operation functions in PHP

Source: Internet
Author: User
In PHP, apart from the most common fopen (), fwrite (), and fread () functions, there are also many useful built-in file operation functions. Some of these functions can be used independently, and some need to work with other functions. Let's take a look at these functions.

In PHP, apart from the most common fopen (), fwrite (), and fread () functions, there are also many useful built-in file operation functions. Some of these functions can be used independently, and some need to work with other functions. Let's take a look at these functions.

1. check whether the file exists: file_exists () function

If you check whether the file exists before opening a file, you can use the file_exists () function. During read-only file operations, this function can be used before the fopen () function to check whether the file to be opened exists without opening the file. This function uses the following example:
If (file_exists ("DOCUMENT_ROOT/../books.txt ")){
Echo 'There have books you can read .';
} Else {
Echo 'There have no Book ';
}

2. determine the file size: filesize () function

The filesize () function can be used to view the size of a file. When downloading a file, we basically need to get the size of the downloaded file. we can use this function to pass the obtained file size to the function that implements the file downloading function. The filesize () function uses the following example:
Echo filesize ("DOCUMENT_ROOT/../books.txt "));

It returns the size of a file in bytes. combined with the fread () function, you can use them to read the entire file (or a part of the file) at a time ). You can use the following code:
$ Fp = fopen ("DOCUMENT_ROOT/../books.txt "));
Echo nl2br (fread ($ fp, filesize ("DOCUMENT_ROOT/../books.txt ")));
Fclose ($ fp );
Note that the nl2br function in the above code converts the n characters output to the line break of HTML (
).

3. delete an object: unlink () function

When the file is not needed, we may delete the file and use the unlink () function. (PHP does not have a function named delete ). The function is used as follows:
Unlink ("DOCUMENT_ROOT/../books.txt "));

If the file cannot be deleted, the function returns false. Generally, if the access permission to the file is insufficient or the file does not exist, the function returns false.

4. locate the rewind (), fseek (), and ftell () functions in the file.

In PHP, you can use the rewind (), fseek (), and ftell () functions to operate the file pointer, or determine the location of the file where it is found.

The rewind () function can reset the file pointer to start the file. The ftell () function reports the current position of the file pointer in bytes. For example, we can add the following lines of code at the end of the script (before the fclose () function ):
$ File = ("file.txt ");
If (is_file ($ file) {// determines whether the file exists
Echo "total file bytes:". filesize ($ file )."
"; // Total number of output bytes
$ Fopen = fopen ($ file, 'RB'); // open the file
Echo "the initial pointer is:". ftell ($ fopen )."
"; // Output pointer position
Fseek ($ fopen, 50); // move the pointer
Echo "position of the pointer after using the fseek () function:". ftell ($ fopen )."
"; // Output the pointer position after moving
Echo "output content after the current pointer:". fgets ($ fopen )."
"; // Output content from the current pointer to the end of the row
If (feof ($ fopen) // determines whether the pointer points to the end of the file.
Echo "the current pointer points to the end of the file:". ftell ($ fopen )."
"; // The position of the output pointer if it points to the end of the file
Rewind ($ fopen); // use the rewind () function
Echo "position of the pointer after the rewind () function is used:". ftell ($ fopen )."
"; // View the position of the pointer after the rewind () function is used
Echo "output content of the current 33 bytes:". fgets ($ fopen, 33); // Output Content of the first 33 bytes
Fclose ($ fopen );
} Else {
Echo "the file does not exist! ";
}
Shows the output of the script in the browser.

You can also use the fseek () function to point the file pointer to a certain position in the file. The function prototype is as follows:
Int fseek (resource fp, int offset, [int whence]);

Call the fseek () function to move the file pointer fp from The whence position to the offset byte. Whence is an optional parameter. its default value is SEEk_SET, which indicates the start position of the file. Other values of this parameter are SEEK_CUR (current position of the file pointer) and SEEK_END (end position of the file ). The rewind () function is equivalent to a fseek () function with zero offset. You can use the fseek () function to find records in the middle of a file or complete a binary search. Generally, if the involved data file has a certain complex program, you can use the database to make the work easier when you must complete these operations.

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.