search
Homephp教程php手册PHP编程中八种常见的文件操作方式
PHP编程中八种常见的文件操作方式Jun 13, 2016 pm 12:36 PM
phpsuperioranddeal withcommonoperatedocumentWayyesserverlocalofTable of contentsprogramming

文件和目录的操作
PHP处理本地服务器上的文件和目录是非常方便的,但有时候会出现权限和路径相关的问题
1.打开文件
resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )
$handle = fopen(filename,mode)//打开文件,返回代表此文件的资源的句柄
文件名称可以使用相对路径或者绝对路径也可以使用网络协议模式,打开模式具有r\r+\w\w+\a\a+\x\x+\b
在操作二进制文件时如果没有指定 'b' 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于 \r\n 字符的奇怪问题。
为移植性考虑,强烈建议在用 fopen() 打开文件时总是使用 'b' 标记。
以下是几种打开文件的方式
$fp = @fopen('log.txt',"rb");
$fp = @fopen('../log.txt',"rb");
$fp = @fopen("http://www.runer.com.cn/default.htm","rb");//还可以使用ftp和ghoper等协议,必须启用php.ini文件中的allow_url_fopen选项
////////////////////////////代码部分////////////////////////////////////////
$filename1 = "userinfo.txt";//目录下或include_path中存在这个文件
$filename2 = "test.txt";//目录下或include_path并不存在这个文件
$resource1 = fopen($filename1,"rb");
@$resource2 = fopen($filename2,"rb");//因为目录中不存在这个文件,并且并未使用或include_path寻找包含文件所在路径则此操作会报错,使用错误抑制符@可以迫使浏览器不输出错误信息
if($resource1)
echo "打开文件{$filename1}成功";
if(!@fopen($filename2,"r"))
echo "打开文件{$filename2}不成功";
//////////////////////////////////////////////////////////////////////////
---------------------输出结果----------------------------------------
打开文件userinfo.txt成功

---------------------------------------------------------------------
2.使用完毕文件后,要显式的告诉PHP已经使用完文件,让操作系统确保将文件的所有内容正确地从缓冲区刷新到硬盘
使用fclose()关闭文件,
bool fclose ( resource handle )//关闭一个已打开的文件指针
3.读取文件,fopen函数的mode参数允许读取,PHP提供了几个函数从文件读取数据
string fgets ( int handle [, int length] )从文件指针中读取一行,在二进制文件上尝试fgets会产生不可预测的结果
如果不指定长度,默认读取1K数据,碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止
string fgetss ( resource handle [, int length [, string allowable_tags]] )从文件指针中读取一行并过滤掉 HTML 标记
fgetc()读取单个字符
fread()读取任意二进制数据
////////////////////////////代码部分////////////////////////////////////////
$handle = fopen ("test.jpg", "rb");
$c;
while (!feof($handle)) {
$contents .= @fread($handle, 8192);//循环读取并将其合并为一个大块文件
}
fclose($handle);
//////////////////////////////////////////////////////////////////////////
---------------------输出结果----------------------------------------

---------------------------------------------------------------------
4.判断文件读取的状态
每个文件句柄都有一个文件指针,或者一个指出下一个操作将在文件中哪里发生的游标,根据fopen函数的mode参数
文件指针最初位于文件的开头(0),或者文件的末尾
feof()可以判断文件是否已经到末尾(到末尾后函数返回TRUE)
filesize()函数返回文件的大小 5.写入文件
fwrite()函数执行文件写入
////////////////////////////代码部分////////////////////////////////////////
$filename = 'test.txt';
$somec;
// 首先我们要确定文件存在并且可写。
if (is_writable($filename)) {
// 在这个例子里,我们将使用添加模式打开$filename,
// 因此,文件指针将会在文件的开头,
// 那就是当我们使用fwrite()的时候,$somecontent将要写入的地方。
if (!$handle = fopen($filename, 'a')) {
echo "不能打开文件 $filename";
exit;
}
// 将$somecontent写入到我们打开的文件中。
if (fwrite($handle, $somecontent) === FALSE) {
echo "不能写入到文件 $filename";
exit;
}
echo "成功地将 $somecontent 写入到文件$filename";
fclose($handle);
} else {
}
echo "文件 $filename 不可写";
//////////////////////////////////////////////////////////////////////////
---------------------输出结果----------------------------------------
成功地将 添加这些文字到文件 写入到文件test.txt
---------------------------------------------------------------------
对于二进制数据,必须指定第三个参数,它包含写入到磁盘的数据字节数
$result = @fwrite($fp,$binary_data,mb_strlen($binary_data,'8bit'));
6.文件权限和其他信息
is_readable()//判断文件是否可读
is_writeable()//判断文件是否可写
is_writable()//判断文件是否可写
fileperms()//判断文件的权限(UNIX风格的文件权限测试函数)
file_exists()//是否存在这个文件
fileowner()//判断文件所属用户
filegroup()//判断文件所属组
7.删除和重命名文件
unlink()//删除文件
rename()//重命名文件
8.访问目录
目录访问建议使用前向斜线"/",兼容windows和unix系统
basename()//返回不包括路径信息的文件名
dirname()//返回文件名的目录部分
realpath()//接受相对路径,返回文件的绝对路径
pathinfo()//提取给定路径的目录名,基本文件名和扩展名
opendir()//打开目录,返回资源句柄
readdir()//读取目录项
rewinddir()//将读取指针返回开头
closedir()//关闭读取句柄
chdir()//改变当前脚本执行期间的当前工作目录
mkdir()//创建目录
rmdir()删除目录
////////////////////////////代码部分////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
---------------------输出结果----------------------------------------

filename: web : filetype: dir

filename: study : filetype: dir

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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools