File operations in PHP are nothing more than file read and write operations, deletion operations, judgment operations, permission operations and some file searches, etc. Let me post the PHP file operation functions I have learned.
File operation function
1. Get the file name: basename();
2. Get the directory where the file is located: dirname();
3. Pathinfo() obtains file information and returns an array, including the path, full name of the file, file name and extension. For example:
The code is as follows | Copy code | ||||
print_r(pathinfo($file)); The result is:Array( [dirname] => /com/netingcn[basename] => error.log [extension] => log[filename] => error )
|
代码如下 | 复制代码 |
$handler = fopen($file, 'w'); // w 会冲掉以前的内容、a 是追加 |
5. Determine whether the directory exists: is_dir();
代码如下 | 复制代码 |
$handler = fopen($file, 'r'); while(!feof($handler)) { while(!feof($handler)) { while(!feof($handler)) { $content = fread($handler, $strLength); //读取指定长读的字符 fclose($handler); |
7. Read all the contents of the file: file() or file_get_contents(), where file() returns an array with one row of elements, and file_get_contents() returns the entire contents of the file as a String; 8. Write the file fwrite, such as:
The code is as follows | Copy code |
$handler = fopen($file, 'w'); // w will flush out the previous content, and a will append
|
代码如下 | 复制代码 |
$file = "phpddt.txt"; $fp = fopen($file,"r"); if ($fp){ while(!feof($fp)){ //第二个参数为读取的长度 $data = fread($fp, 1000); } fclose($fp); } echo $data; ?> |
The code is as follows | Copy code |
$handler = fopen($file, 'r'); while(!feof($handler)) { $datas[] = fgets($handler); //Read a line of content } while(!feof($handler)) { $datas[] = fgetss($handler); //Read a line of content and add the html tag } while(!feof($handler)) { $datas[] = fgetcsv($handler); //Read a line of content and parse the csv field } $content = fread($handler, $strLength); //Read the characters of the specified long read fclose($handler); |
The code is as follows | Copy code |
$file = "phpddt.txt";<🎜> $fp = fopen($file,"r");<🎜> if ($fp){<🎜> while(!feof($fp)){<🎜> //The second parameter is the read length<🎜> $data = fread($fp, 1000);<🎜> }<🎜> fclose($fp);<🎜> }<🎜> echo $data;<🎜> ?> |
Run result:
PHP Diandiantong (www.bKjia.c0m), focuses on PHP development and provides professional PHP tutorials!
2.fseek (resource handle, int offset [, int whence]), offset the pointer to offset offset.
(The content of php.txt is [Welcome to www.bKjia.c0m])
After running the following php code:
The code is as follows | Copy code | ||||
$file = "php.txt"; |
代码如下 | 复制代码 |
|
to www.bKjia.c0m
The description of whence parameter is as follows:
SEEK_SET - Set position equal to offset bytes.
SEEK_CUR - Sets the position to the current position plus offset.
SEEK_END - Set the position to the end of the file plus offset. (assignment)
If whence is not specified, the default is SEEK_SET.
3. The ftell() function is used to obtain the offset of the pointer position
The php demo code is as follows:
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$file = "phpddt.txt"; $fp = fopen($file,"r"); //Jump the file pointer to after the 8th byte
//Get the offset of the pointer position
First save the phpddt.ini file. The content of the file is as follows: name = Baidu search Write the following php code:
This is a program that returns the files and directories under the file directory (0 files will return false). Sometimes you need to know directory information. You can use dirname($path) and basename($path) to return the directory part and file name part of the path respectively. You can use disk_free_space($path) to return the free space. Create command:
rmdir($path) The file with the path in $path will be deleted. dir -- directory class is also an important class for operating file directories. It has three methods, read, rewind, and close. This is a pseudo-object-oriented class. It first uses open file handles, and then uses pointers. Read., see the php manual here:
Output: Handle: Resource id #2 Path: /etc/php5
. cgi cliFile attributes are also very important. File attributes include creation time, last modification time, owner, file group, type, size, etc.
Let’s focus on file operations.
However, the file_get_contents function is not supported on lower versions. You can first create a handle to the file, and then use a pointer to read the entire file: $fso = fopen($cacheFile, 'r'); There is another way to read binary files: $data = implode('', file($file)); ● Write file Same as reading a file, first check if it can be written:
|