🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1、is_executable():判断某个文件是否是可执行文件,返回bool类型。 if(is_executable('bb.exe')){ echo '这是一个可执行文件,它的大小是'.filesize('bb.exe')/pow(1024,2).'<br/>'; } 2、file_exists():判断某个文件是否存在,返回bool类型。 if(file_exists('bb.exe')){ echo '文件存在<br/>'; } 判断某个文件是否可读、可执行、是否是一个文件,首先会判断这个文件是否存在。 3、fopen():打开某个文件、fread():读取某个文件里边的内容、rwrite():往某个文件里边写入内容,会覆盖原有内容。 (1)【r】只读模式: $file = fopen('a.txt','r'); echo fread($file,filesize('a.txt')); fclose($file); (2)【r+】读写模式: $file = fopen('a.txt','r+'); fwrite($file,'246810'); fclose($file); (3)【w】只写模式: 用w只写模式fopen一个文件,如果这个文件存在,则把里边的内容全部清空,光标定位在第一位,如果这个文件不存在,将会自行创建一个文件。 $file = fopen('a.txt','w'); fwrite($file,'hello world'); rewind($file); echo fread($file,filesize('a.txt')); (4)【w+】写读模式: 执行写入操作之后,光标定位在文字的最后,这个时候需要用rewind()函数把光标重置到第一位,这样才能读到内容。 $file = fopen('a.txt','w+'); fwrite($file,'写读模式'); rewind($file);//写入之后把光标重置到第一位 echo fread($file,filesize('a.txt')); (5)【a】只写模式: 与【w】只写模式类似,但是它们之间的区别是:w只写模式会把原文件里边的内容清空,但是a只写模式不会。 $file = fopen('a.txt','a'); fwrite($file,'hello world'); (6)【a+】写读模式: $file = fopen('a.txt','a+'); fwrite($file,'hello world'); rewind($file); echo fread($file,filesize('a.txt')); (7)【x】只写模式: 如果已经存在了要fopen打开的文件,那么就会报错不会执行写入和读取,如果不存在,就直接创建一个这样的文件,然后执行写入和读取操作。 $file = fopen('aa.txt','x'); fwrite($file,'hello world'); (8)【x+】写读模式: $file = fopen('aa.txt','x'); fwrite($file,'hello world'); rewind($file); echo fread($file,filesize('aa.txt')); (9)利用fopen()读取远程文件: 执行以下代码,如果出错,可以在php配置文件中修改【allow_url_fopen=On】,并且重新服务器即可! $file = fopen('http://www.baidu.com','r'); while($row = fgets($file)){ echo $row; } (10)读取二进制图片: header('content-type:image/jpg'); $file = fopen('me.jpg','rb'); echo fread($file,filesize('me.jpg')); 工作中用的最多的是【r】模式和【w】模式。