时间:2021-07-01 10:21:17 帮助过:23人阅读
<?php
#判断文件类型是:文件
$file='img/meinv.jpg';
echo filetype($file);
?><?php
#判断文件类型是:目录
$file='img';
echo filetype($file);
?><?php
#判断到底是不是文件:是
$file='img/meinv.jpg';
echo is_file($file);
?><?php
#判断到底是否存在:是
$file='img/meinv.jpg';
$b=file_exists($file);
var_dump($b);
?><?php
#获取文件大小M
$file='img/meinv.jpg';
$b=filesize($file);
var_dump($b);
?><?php
#读取三个字节
$file='./a.txt';
$fp=fopen($file,'r');
$str=fread($fp,3);
echo $str;
?><?php
#读取三个字节
$file='./a.txt';
$fp=fopen($file,'r');
$str=fread($fp,6);#换行的时候隐藏了个\n(占据两个字节)
echo $str;
?><?php
#获取文件里所有东西
$file='a.txt';
$size=filesize($file);
$fp=fopen($file,'r');
$str=fread($fp, $size);
echo $str;
?><?php
$file='b.txt';
$fp=fopen($file,'w');#生成一个b.txt文件 a:追加文本,w重新写
$str='aa1<br>bb<br>cc<br>';
fwrite($fp,$str);#往文件里写入字符串
?><?php
$file='b.txt';
unlink($file);#删除文件函数
?><?php
$sfile='a.txt';
$dfile='aaa.txt';
rename($sfile,$dfile);#文件重命名
?><?php
$sfile='aaa.txt';
$dfile='ccc.txt';
copy($sfile, $dfile);#文件复制函数
?><?php
$sfile='aaa.txt';
$dfile='ddd.txt';
copy($sfile, $dfile);#文件复制函数
#把源文件删除了整合一下就是复制
#后把源文件删除就是移动
unlink($sfile);
?> <?php
$sfile='ccc.txt';
readfile($sfile);#直接读取文件里内容并输出函数
?><?php
$sfile='ccc.txt';
$str=file_get_contents($sfile);#只是读取不输出
?><?php
$sfile='http://www.baidu.com';
$str=file_get_contents($sfile);#只是读取不输出
echo $str; #把读取的百度网址输出,呈现百度界面
?> <?php
$sfile='http://www.baidu.com';
$str=file_get_contents($sfile);#只是读取不输出
$ptn='/<title>(.+)<\/title>/';#正则匹配采集条件
preg_match($ptn,$str,$arr);#采集函数
echo "<pre>";
print_r($arr);#输出采集内容
echo "</pre>";
?>以上就是PHP文件操作的代码示例的详细内容,更多请关注Gxl网其它相关文章!