您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP完全手册- 文件系统

2025/7/31 16:48:51发布19次查看
1.  目录操作
$dir="c:"; if(is_dir($dir)){ 验证路径的有效性 $dir_res=opendir($dir); 返回一个资源对象,用于存储当前的目录资源 while($filen=readdir($dir_res)){ 读取目录中的文件 echo $filen."<br>"; } closedir($dir_res);关闭目录 } else echo "目录不存在!"; $dir2="test/"; if(!is_dir($dir2)){ mkdir($dir2); 创建目录 } if(is_dir($dir2)){ rmdir($dir2); 删除目录 }
2. 文件读取
$path="test\\1.txt"; $filesize=filesize($path);#获取文件的长度 $file=fopen($path, "r");#打开文件 echo $filesize."字节<br>"; echo fgetc($file)."<br>";#读取一个字符, echo fgetc($file)."<br>";#读取后指针下移 echo fgets($file)."<br>";#从指针出开始读取一行 echo fgets($file)."<br>"; echo fread($file, $filesize); #从指针出开始读取指定长度的字符串 fclose($file);
3. 文件写入
$path="test\\1.txt"; $filesize=filesize($path);#获取文件的长度 $file=fopen($path, "w"); fwrite($file, "hello world!\n",20);#写入的字符串,要写入的长度 fwrite($file, "this is a test!\n"); fclose($file); unlink($path);#删除文件
4. fopen文件时,若文件不存在,则自动创建该文件
$path="db/"; $filename="s".date("ymdhis").".dat"; $fp=fopen($path.$filename, "w");
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product