PHP开发笔记系列(四)-文件操作
??? 对于一般的web应用程序,数据会保存在数据库表中,但是文件操作也是必须的,例如文件的读取、写入等,典型的应用场景是考试报名,当系统启动时,自动读取“报名须知”的内容到内存中,然后再需要时显示到页面当中。这次本文《php开发笔记系列(四)-文件操作》将研究一下php中的文件操作。
?
1. 读取文件到数组(file)
??? file()函数可以将文件按行读取到一个字符串数组中,后续程序可以通过遍历这个字符串数组来做处理。
'; }?>
?
2. 读取文件到字符串(file_get_contents)
??? file_get_contents()函数可以将文件中的所有内容读取到一个字符串变量中,后续程序可以将这个字符串中的内容显示在页面,或者写入到新文件当中。
立即学习
“
PHP免费学习笔记(深入)
”;
file:file.phpurl:http://localhost:88/file/file.php'; echo $i.':'.htmlspecialchars($file[$i]); echo 'file:file_get_contents.phpurl:http://localhost:88/file/readfile.php';?>file:readfile.phpurl:http://localhost:88/file/readfile.phpfile:fread.phpurl:http://localhost:88/file/fread.phpfile:fgets.phpurl:http://localhost:88/file/fgets.phpfile:readOnce_writeOnce.phpurl:http://localhost:88/file/readOnce_writeOnce.phpfile:multiRead_multiWrite.phpurl:http://localhost:88/file/multiRead_multiWrite.phpfile:other.phpurl:http://localhost:88/file/other.php'; // is_file()返回1 或 0 echo $fileName.' is '.is_file($fileName)?'file':'directory'.'!'.'
'; // is_readable()返回1 或 0 echo $fileName.' readable:'.is_readable($fileName).'
'; // is_writable()返回1 或 0 echo $fileName.' writable:'.is_writable($fileName).'
'; echo $fileName.' created at:'.filemtime($fileName).'
'; echo $fileName.' created at:'.fileatime($fileName).'
'; echo $fileName.' size:'.filesize($fileName).'
';?> file:dir.phpurl:http://localhost:88/file/dir.php'; }elseif (is_file($entry)) { echo '[FILE] '.$entry. '
'; } } closedir($dp);?>file:dir-class.phpurl:http://localhost:88/file/dir-class.phpread()){ if(is_dir($entry)) { echo '[DIR] '.$entry. '
'; }elseif (is_file($entry)) { echo '[FILE] '.$entry. '
'; } } closedir($dp);?>file:copy.phpurl:http://localhost:88/file/copy.php