PHP file Basic Operation

The basic file operations :( more )

fopen (): File Open
$file = fopen("file.txt","r+"); 

Parameters fopen () function is the read-write mode and file path of the target file; while the fopen function will find no files will automatically create a file in the file open mode when it must be read or write mode

fread (): File Read
fread($file,filesize("file.txt"));

fread () function parameter maximum number of bytes to be read are read files and

fgets (): read the single-line file
fgets($file);

Read a single line of text in the file, no one will read the file pointer at the head of the next line

fgetc (): Read a single character
fgetc($file);

fgetc () function will read the contents of the output by one character; while loop is a combination of common output, using the EOF check function feof () to control the loop

fclose (): File Close
fclose($file);

Once the file is opened it must be closed after use, in principle,

fopen () function to open a file assigned to a variable file, the equivalent file is the address of the file pointer, fread () function in accordance with the address pointer file can be found in the file they need to read (the same token, you can fread () function direct use fopen () function, open the file directly, tell fread () file address, and then perform their function character)

PHP File Checker (EOF):
feof (): check whether the end of the file
feof($file);

The main function feof () function is to check the file pointer has reached the end of the file, to solve the unknown size of the read and write can take to prevent cross-border issues

fwrite (): write to file
    <?php
        $file = fopen("/File.txt","w");// w:文件写入模式;如果没有该文件就会自动创建文件
        $txt = "Chinawangyuyang\r\nGMAIL.com";//以String类写入,并运用转义转义字符实现简单排版
        fwrite($file, $txt);//参数:写入路径,写入的内容
        fclose($file);
    ?>

fwrite () function file address of the primary parameters are written and a character string or a variable type to be written

Note: file write and file operations (read-write) mode has a very important relationship; fwrite () function is only responsible for writing data to a file, and fopen () function each time you open a file when it will be a pointer to the file header or directly to the new data to cover all or part of the data of the original data; when we are not in the coverage of an existing file data file deletions to note fopen () for read-write mode is selected by adding or specified file pointer location from which to start writing pointer position data;

Other file functions:

file_exists (): file exists
file_exists("file.txt");

(Without opening the file) detecting whether the file exists; parameter is an absolute or relative path to a file address; returns a Boolean value

filesize (): the file size in bytes
filesize("file.txt");

Detecting the size in bytes of the file, the parameters relative or absolute path to a file address;

unlink (): Delete Files
unlink("file.txt");

Delete the specified file; parameter is the address of a relative or absolute path to the file; returns a Boolean value

File pointer positioning:
rewind (): neck
  • The file pointer is reset to the file header
ftell (): returns the position
  • The current position of the file pointer return
fseek (): specified position
  • The pointer to a specific position

    int fseek( resource fp , int offset [, int whence]);

    fseek () function is the main function the file pointer fp whence position offset from byte; (default is the first portion The whence [SEEK_SET] file; current pointer position [SEEK_CUR]; end of file [SEEK_END])

flock (): file locking
bool flock( resource fp , int operation [, int &wouldblock]);

* Accomplished by flock () function of file locks; Main parameters: specify a file pointer, and the constant need for a locking type; returns a Boolean value (if the file is successfully locked);

Guess you like

Origin www.cnblogs.com/wangyuyang1016/p/11470584.html