一,文件读取写入
1,file_get_contents读取
<?php
$filenam="hello.php";
$filestring=file_get_contents($filenam); //打开html文件就会直接执行
echo $filestring;
?>
2,readfile读取文件
<?php
readfile('hello.php')
?>
3,fopen,fread/fwrite,fclose读取/写入文件
(1)读取文件
<?php
$file=fopen('hello.php','r');
$rfile=fread($file,filesize("hello.html")); // 两个参数,读取所有
echo "$rfile";
fclose($file);
?>
(2)写入文件
<?php
$file=fopen('hello.php','w');
$wfile=fwrite($file,"hello");
fclose($file);
print $wfile; // 有多少字节被写入了
?>
4,file_put_contents写入文件
<?php
// 写入文件返回写入字节数
$data="你好啊,php";
$numbates=file_put_contents('aa.txt',$data);
echo'==============='.'<br>';
echo "$numbates";
?>
5,创建临时文件
使用完后删除,不需要维护文件删除状态
<?php
$handle=tmpfile(); //创建临时文件
fwrite($handle,"deaff"); //向临时文件中写入
?>
二,文件其它操作
1,重命名文件
rename('hello.php','helloword.php'); //把hello.php文件名改为helloword.php
2,复制文件
copy('hello.php','hello_copy.php');
3,删除文件
unlink('hello_copy.php');
三,文件属性
1,检测文件是否存在:
file_exists
2,检测文件是否可读/写/执行
is_file() 是否是文件
is_readable($filename) 文件是否可读
is_writeable($filename) 是否可写
is_writable($filename) 是否可写返回1,0
is_executable($filename) 是否可执行