Home  >  Article  >  Backend Development  >  Introduction to commonly used file operation functions in php_PHP tutorial

Introduction to commonly used file operation functions in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:21990browse

Today, the editor will introduce to PHP beginners a summary of the common functions used in PHP file operations, including: file reading and writing, creation, viewing file attributes, file deletion and other file operations.

Before accessing a file, we generally need to determine whether the file exists to avoid calling a non-existent file and causing an error.

PHP function to determine whether a file exists: file_exists(), the structure is as follows:

file_exist($string);

The parameter $string is a character variable pointing to a file or directory. If the file or directory exists, it returns true, otherwise it returns false.

Example:

The code is as follows Copy code
 代码如下 复制代码

/* 判断post.php是否存在 */
$file="post.php";
if(file_exists($file)){
echo "文件存在
";
 }
 else{
  echo "文件不存在
";
 }
 
 /* 判断images目录是否存在 */
 $category="images";
 if(file_exists($category)){
  echo "目录存在";
 }
 else{
  echo "目录不存在";
 } 
?>

/* Determine whether post.php exists */
$file="post.php";
if(file_exists($file)){
echo "File exists
";
}
else{
echo "File does not exist
";
}

/* Determine whether the images directory exists */
$category="images";
if(file_exists($category)){
echo "Directory exists";
}
else{
echo "Directory does not exist";
}
?>

php provides some functions for accessing file attributes, which can obtain the file size, type, modification time, etc.

获取文件属性函数

函数名 作用 参数及返回值
filesize($string) 获取文件大小 参数$string为一个指向文件或目录的字符型变量。函数的返回值为整型变量,返回文件的大小,返回结果会被缓存。如果出错,则返回false。函数参数不能为远程文件。
filetype($string) 获取文件类型 参数$string为一个指向文件或目录的字符型变量。函数的返回值为字符型变量,返回结果会被缓存。
filemtime($string) 获取文件修改时间 参数$string为一个指向文件或目录的字符型变量。函数的返回值为整型变量,返回文件的修改时间。
fileatime($string) 获取文件访问时间 参数$string为一个指向文件或目录的字符型变量。函数的返回值为整型变量,返回文件的访问时间。
fileperms($string) 获取文件权限 参数$string为一个指向文件或目录的字符型变量。函数的返回值为整型变量,返回文件的相应权限,返回结果会被缓存。函数参数不能为远程文件。

Get file attribute function

Function name Function Parameters and return values
filesize($string) Get file size The parameter $string is a character type variable that points to a file or directory. The return value of the function is an integer variable, returning the size of the file, and the return result will be cached. If an error occurs, false is returned. Function parameters cannot be remote files.
filetype($string) Get file type The parameter $string is a character variable pointing to a file or directory. The return value of the function is a character variable, and the return result will be cached.
filemtime($string) Get file modification time The parameter $string is a character variable pointing to a file or directory. The return value of the function is an integer variable and returns the modification time of the file.
fileatime($string) Get file access time The parameter $string is a character variable pointing to a file or directory. The return value of the function is an integer variable and returns the access time of the file.
fileperms($string) Get file permissions The parameter $string is a character variable pointing to a file or directory. The return value of the function is an integer variable and returns the corresponding permissions of the file. The return result will be cached. Function parameters cannot be remote files.

Example:
 代码如下 复制代码

 $filename="php.txt";
 echo filesize($filename). "
";
 echo filetype($filename). "
";
 echo date("Y年 m月 d日",filemtime($filename)). "
";
 echo date("Y年 m月 d日",fileatime($filename)). "
";
 echo fileperms($filename). "
";
?>

The code is as follows Copy code
$filename="php.txt";
echo filesize($filename). "
";
echo filetype($filename). "
";
echo date("Y year m month d day",filemtime($filename)). "
";
echo date("Y year m month d day",fileatime($filename)). "
";
echo fileperms($filename). "
";
?>

Before reading a file, you must first open a file. PHP provides the fopen() function to open local files or remote files. Its basic structural form is as follows:

resource fopen (string $filename, string $mode)
The parameter filename is the name of the file to be opened. The parameter mode is the way to open the file, as shown in the following table:

Description of mode parameter in fopen()
fopen()中的mode参数说明
mode 说明
r 只读方式打开,将文件指针指向文件头。
r+ 读写方式打开,将文件指针指向文件头。
w 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
w+ 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
a 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
a+ 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
x 创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。
x+ 创建并以读写方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。
mode Description
r Open in read-only mode and point the file pointer to the file header.
r+ Open in read-write mode and point the file pointer to the file header.
w Open in writing mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.
w+ Open in read-write mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.
a Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
a+ Open in read-write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
x is created and opened for writing, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT flag for the underlying open(2) system call. This option is supported by PHP 4.3.2 and later, and can only be used for local files.
x+ is created and opened for reading and writing, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT flag for the underlying open(2) system call. This option is supported by PHP 4.3.2 and later, and can only be used for local files.

The fopen() function returns a value that contains an integer file handle and is used to identify the file to the function that performs the file operation. This value is often called a pointer, which is like a door to a room in memory. If php fails to open the file, then this value is false.

Example:

The code is as follows Copy code
 代码如下 复制代码


  fopen("php.txt","a"); /* 写入方式打开本地文件 */
fopen("http://www.bKjia.c0m/robots.txt","r"); /* 只读方式打开远程服务器文件 */
?>

fopen("php.txt","a"); /* Open local file in writing mode */

fopen("http://www.bKjia.c0m/robots.txt","r"); /* Open the remote server file in read-only mode */

?>

First create a "php.txt" file with the following content:

hello

php

1. The fgetc() function reads a certain character in the file. Its structure is as follows:

string fgetc(resource $handle)
 代码如下 复制代码

$filename=fopen('php.txt','r');
$string=fgetc($filename); /* 读取文件开头第一个字符 */
echo $string;
?>

 

The parameter $handle is the file pointer that has been opened, and the function returns the character pointed to by the current file pointer. If the file pointer points to the end of the file, returns false. Example:
The code is as follows Copy code
$filename=fopen('php.txt','r');<🎜> $string=fgetc($filename); /* Read the first character at the beginning of the file */<🎜> echo $string;<🎜> ?>

After the file is opened, the file pointer is usually located at the beginning of the file. But when certain operations are performed on the file, it is difficult to determine the position of the php pointer at this time.

php file pointer position search function ftell(), its structure is as follows:


int ftell(resource $handle)
The parameter $handle is the file where the pointer is to be found. This function can determine the position of the file pointer, and the function returns an integer value.

Example:

The code is as follows
 代码如下 复制代码

$f=fopen("php.txt","r");
fgets($f,2);
echo ftell($f);
?>

Copy code

$f=fopen("php.txt","r");

fgets($f,2);

echo ftell($f);

?>

 代码如下 复制代码

 $filename="php.txt";
 $str1="第一次写入
";
 $str2="第二次写入";
 if(is_writable($filename)){  /* is_writable()函数判断文件是否可写 */
  $file=fopen($filename,"w"); /* 以写入方式打开文件 */
  $w1=fwrite($file,$str1); /* 将内容写入文件 */
  $w2=fwrite($file,$str2);
   $file=fopen($filename,"r"); 
   if($w1) echo fgets($file); /* 读取文件内容 */
   else echo "写入不成功"; 
 }
 else echo "文件不可写";
?>


php file writing function fwrite() can write the required content into the target file. The structure is as follows:


int fwrite(resource $handle,string $string [,int $length])

The parameter $handle is the file to be written, the adopted number $string is the content to be written, and the parameter $length is optional and is the length to be written. The fwrite() function returns the number of characters written, and returns false if an error occurs.

Example:

The code is as follows
 代码如下 复制代码

 $f=fopen("php.txt","r");
 echo fgets($f)."
";  /*输出第一行*/
 echo fgets($f)."
";  /*输出第二行*/
 rewind($f);             /*指针返回文件头*/
 echo fgets($f);         /*输出第一行*/   
?>

Copy code
$str1="First write
";

$str2="Second write"; $file=fopen($filename,"w"); /* Open the file for writing */ $w1=fwrite($file,$str1); /* Write the content to the file */ $w2=fwrite($file,$str2); $file=fopen($filename,"r"); If($w1) echo fgets($file); /* Read file content */ else echo "Write failed"; } else echo "File cannot be written"; ?>
The PHP pointer function rewind() can set the file location pointer to the beginning of the file. Its structure is as follows:
bool rewind (resource $handle ); The function returns a Boolean value, true if successful, false if failed. Example:
The code is as follows Copy code
"; /*Output the first line*/ echo fgets($f)."
"; /*Output the second line*/ rewind($f); /*The pointer returns the file header*/ echo fgets($f); /*Output the first line*/ ?> http://www.bkjia.com/PHPjc/628845.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628845.htmlTechArticleThe editor today will introduce to PHP beginners a summary of the common functions used for PHP file operations, including: file reading and writing , create, view file attributes, file deletion and other operations on files. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn