PHP提供fopen()函数来打开文件。甚至 fopen() 它还可以打开不同类型的资源和协议,如HTTP、HTTPS、FTP等。打开一个文件看起来很简单,但它可能有不同的文件打开模式,如读写、只读、打开文件(如果不退出创建等)。fopen()是从C编程语言派生的,大多数属性都是从C派生的。
fopen()函数语法
fopen()函数具有以下语法。即使有4个参数,通常也会在调用fopen()函数时使用两个参数。
fopen( $filename , $mode , $use_include_path , $context)
- $文件名 是提供要打开的文件、URL或类似名称和方案的最重要参数$文件参数是必需的。
- $模式 参数是指定文件或资源打开模式的另一个必需参数。文件可以以只读、读/写等方式打开。下面详细说明文件打开模式。
- $useu includeu路径 是一个很少使用的可选参数。
- $上下文 是一个很少使用的可选参数。
文件打开模式
fopen()函数可用于以不同模式打开文件。$mode参数是fopen()函数的第二个参数,是打开文件所必需的。下面我们将列出fopen()函数的所有可能模式。mode参数以单引号或双引号形式提供。
'r' |
仅供阅读;将文件指针放在文件的开头。 |
'r+' |
开放阅读和写作;将文件指针放在文件的开头。 |
'w' |
只开放供书写;将文件指针放在文件的开头,并将文件截断为零长度。如果文件不存在,请尝试创建它。 |
'w+' |
开放阅读和写作;将文件指针放在文件的开头,并将文件截断为零长度。如果文件不存在,请尝试创建它。 |
'a' |
只开放供书写;将文件指针放在文件末尾。如果文件不存在,请尝试创建它。在这种模式下, fseek() 没有效果,写入总是附加的。 |
'a+' |
开放阅读和写作;将文件指针放在文件末尾。如果文件不存在,请尝试创建它。在这种模式下, fseek() 只影响读取位置,写入总是附加的。 |
'x' |
只为写作而创建和打开;将文件指针放在文件的开头。如果文件已经存在,则 fopen() 呼叫将因返回而失败 FALSE 产生一个水平误差 E_WARNING . 如果文件不存在,请尝试创建它。这相当于指定 O_EXCL|O_CREAT 底层的标志 open(2) 系统调用。 |
'x+' |
创造并开放阅读和写作;否则它的行为与 'x' . |
'c' |
只打开文件进行写入。如果文件不存在,则创建该文件。如果存在,则既不截断(与 'w' ),对该函数的调用也不会失败(与 'x' ). 文件指针位于文件的开头。如果希望获得一个通知锁,这可能很有用(请参见 flock()),然后尝试修改文件,如使用 'w' 无法在获取锁之前截断文件(如果需要截断, ftruncate() 可在请求锁后使用)。 |
'c+' |
打开文件进行读写;否则它的行为与 'c' . |
用fopen()函数打开文件
我们将从一个简单的例子开始,我们将以不同的方式打开文件。我们将打开文件及其绝对路径,相对路径,Windows文件等。
<?php
//Open file readonly and place pointer at the beginning of file $myfile = fopen("myfile.txt","r");
//Open file read and write, place pointer at the beginning of file
$myfile = fopen("myfile.txt","r+");
//Open file for write-only and create if doesn't exist and overwrite if exist
$myfile = fopen("myfile.txt","w");
//Open file and create if doesn't exist and overwrite if exist
$myfile = fopen("myfile.txt","w+");
//Open file and create if doesn't exist and overwrite if exist
$myfile = fopen("/home/ismail/myfile.txt","w+");
//Open file in Windows $myfile = fopen("c://myfile.txt","w+");
?>
使用fopen()函数以只读方式打开文件
文件可以以不同的模式打开以进行不同的操作。但是只读模式是最流行的模式之一,在这种模式下,打开的文件的内容可以读取,但不能编辑。我们将为Linux和Windows操作系统提供以只读模式打开具有绝对路径和相对路径的文件的示例。
<?php //Open file readonly and place pointer at the beginning of file
$myfile = fopen("myfile.txt","r");
//Open file read and write, place pointer at the beginning of file
$myfile = fopen("myfile.txt","r");
//Open file read-only with absolute path $myfile = fopen("/home/ismail/myfile.txt","r");
//Open file in Windows
$myfile = fopen("c://myfile.txt","r");
?>
使用fopen()函数打开文件并创建不存在的文件
另一个在PHP中打开文件的流行情况是尝试打开一个文件,如果该文件不存在,则创建并打开一个要写入的文件。这个 w型+ 模式可用于打开文件进行写入,如果不存在,则创建并打开文件进行写入。
<?php
//Open file for writing and create if it does not exist $myfile = fopen("myfile.txt","w+");
//Open file with absolute path for writing and create if it does not exist $myfile = fopen("/home/ismail/myfile.txt","w+");
//Open file in Windows with absolute for writing and create if it does not exist $myfile = fopen("c://myfile.txt","w+");
?>
使用fopen()函数打开网页(HTTP)
甚至fopen()函数也是为打开磁盘和文件系统上的文件而创建的,后来它还获得了一些额外的功能,比如打开网页或HTTP或HTTPS协议。为了打开一个网页,URI方案应该提供http或https,然后提供完整或绝对路径。我们将使用“r”模式,因为我们不能更改提供的URI或web页面。
<?php
//Open specified HTTP URL or web page $myfile = fopen( "http://www.wisetut.com" , "r" );
//Open specified HTTPS URL or web page
$myfile = fopen( "https://www.wisetut.com" , "r" );
?>
用fopen()函数打开FTP
FTP或文件传输协议是另一种流行的internet协议,用于通过FTP服务在internet或服务上传输文件。fopen()方法可用于打开ftpuri,如下所示。如果远程FTP需要使用用户名和密码进行身份验证,我们可以通过FTP URI提供用户名和密码。我们可以使用“r”和“w”模式在FTP服务器上读写文件。
<?php
//Open specified FTP file to read $myfile = fopen( "ftp://www.wisetut.com/file.txt" , "r" );
//Open specified FTP file to write $myfile = fopen( "ftp://www.wisetut.com/file.txt" , "w" );
//Open specified FTP file with authentication providing username and pass $myfile = fopen( "ftp://user:[email protected]/file.txt" , "w" );
?>
使用fopen()函数打开目录
我们能用fopen()打开目录吗?是的,即使fopen()设计为打开文件,它也可以打开目录,因为从Linux文件系统的角度来看,系统上的所有东西都是一个文件。目录也是一个文件,可以用fopen()函数打开。
<?php
//Open specified Directory or Folder read
$myfile = fopen( "/home/ismail" , "r" );
//Open specified Directory or Folder to read $myfile = fopen( "ismail" , "r" );
?>
用fclose()函数关闭打开的文件
打开文件将锁定打开的文件并占用一些系统资源来处理文件。为了正确地完成打开的文件操作,应该使用fclose()函数关闭文件。正确关闭文件也会使更改或任何写操作持久化。
<?php
//Open specified Directory or Folder read
$myfile = fopen( "/home/ismail" , "r" );
//Close file flose($myfile);
?>