In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "introduction of common file operations of PHP", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "PHP common file operation introduction" bar!
1. Open a file
Resource fopen (string filename, string mode [, bool use_include_path [, resource zcontext]]) $handle = fopen (filename,mode) / / Open the file and return the handle to the resource that represents the file
The file name can be a relative path or an absolute path or a network protocol mode, and the open mode has r\ r +\ w\ w +\ a +\ x\ x +\ b
If you do not specify the'b 'flag when manipulating binaries, you may encounter some strange problems, including broken picture files and strange problems with\ r\ ncharacters.
For portability, it is strongly recommended that you always use the'b 'tag when opening files with fopen ().
Here are several ways to open a file
$fp = @ fopen ('log.txt', "rb"); $fp = @ fopen ('.. / log.txt', "rb"); $fp = @ fopen ("http://www.runer.com.cn/default.htm","rb");// can also use protocols such as ftp and ghoper, and the allow_url_fopen option $filename1 =" userinfo.txt "in the php.ini file must be enabled; / / the file $filename2 =" test.txt "exists in the directory or in include_path / / the file $resource1 = fopen ($filename1, "rb") does not exist in the directory or include_path; @ $resource2 = fopen ($filename2, "rb"); / / because the file does not exist in the directory and is not used or include_path looks for the path of the included file, this operation will report an error. Use the error suppressor @ to force the browser not to output the error message if ($resource1) echo "Open file {$filename1} successfully" If (! @ fopen ($filename2, "r") echo "failed to open the file {$filename2}"
Output: file userinfo.txt opened successfully
two。 After using the file, explicitly tell PHP that you have finished using the file, and let the operating system ensure that all the contents of the file are flushed correctly from the buffer to the hard disk.
Use fclose () to close the file
Bool fclose (resource handle) / / close an open file pointer
3. Read the file, the mode parameter of the fopen function allows reading, and PHP provides several functions to read data from the file
String fgets (int handle [, int length]) reads a line from the file pointer, and trying fgets on the binary will produce unpredictable results
If the length is not specified, 1K of data is read by default and stops when it encounters a newline character (included in the return value), EOF, or has read length-1 bytes
String fgetss (resource handle [, int length [, string allowable_tags]]) reads a line from the file pointer and filters out the HTML tag
Fgetc () reads a single character
Fread () reads arbitrary binary data
Code part:
$handle = fopen ("test.jpg", "rb"); $c; while (! feof ($handle)) {$contents. = @ fread ($handle, 8192); / / read and merge it into one chunk file} fclose ($handle)
4. Determine the status of the file read
Each file handle has a file pointer, or a cursor indicating where the next operation will occur in the file, according to the mode parameter of the fopen function
The file pointer is initially located at the beginning of the file (0), or at the end of the file
Feof () can determine whether the file has reached the end (the function returns TRUE at the end)
The filesize () function returns the file size 5. 0. Write to a file
Write to the fwrite () function execution file
Code part:
$filename = 'test.txt'; $somec; / / first we need to make sure the file exists and is writable. If (is_writable ($filename)) {/ / in this example, we will open $filename using add mode, / / so the file pointer will be at the beginning of the file, / / that's where $somecontent will be written when we use fwrite (). If (! $handle = fopen ($filename,'a')) {echo "cannot open the file $filename"; exit;} / / writes $somecontent to the file we opened. If (fwrite ($handle, $somecontent) = FALSE) {echo "cannot write to file $filename"; exit;} echo "successfully wrote $somecontent to file $filename"; fclose ($handle);} else {} echo "file $filename is not writable"
Output result:
Successfully add these words to the file and write to the file test.txt
For binary data, you must specify a third parameter that contains the number of bytes of data written to disk
$result = @ fwrite ($fp,$binary_data,mb_strlen ($binary_data,'8bit'))
6. File permissions and other information
Is_readable () / / determines whether the file is readable
Is_writeable () / / determine whether the file is writable or not
Is_writable () / / determine whether the file is writable or not
Fileperms () / / determine the permissions of the file (UNIX-style file permission test function)
File_exists () / / whether this file exists
Fileowner () / / determine the user to which the file belongs
Filegroup () / / determine the group to which the file belongs
7. Delete and rename files
Unlink () / / Delete files
Rename () / / rename the file
8. Access directory
Directory access is recommended to use a forward slash "/", which is compatible with windows and unix systems
Basename () / / returns the file name that does not include path information
Dirname () / / returns the directory portion of the file name
Realpath () / / accepts the relative path and returns the absolute path of the file
Pathinfo () / / extract the directory name, base file name and extension of the given path
Opendir () / / Open the directory and return the resource handle
Readdir () / / read directory entries
Rewinddir () / / returns the read pointer to the beginning
Closedir () / / close the read handle
Chdir () / / change the current working directory during execution of the current script
Mkdir () / / create a directory
Rmdir () delete directory
Thank you for your reading, the above is the "introduction of common file operations of PHP" content, after the study of this article, I believe that you have a deeper understanding of the common file operation of PHP to introduce this problem, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.