<?php header("Content-Type:text/html;charset=utf-8"); /*** * 目录操作。 * 磁盘 目录和文件计算。 * 文件处理。 */ /*** * 目录文件。获取目录 解析目录 * 文件的扩展名 文件后缀 * basename 文件名 * dirname 目录部分 * pathinfo 文件名+后缀名 * */ $path='/Applications/MAMP/htdocs/demo6/demo6_0.php'; echo $path; echo "<br/>"; echo basename($path); echo dirname($path); echo "<br/>"; $arraysPath=pathinfo($path); echo $arraysPath["dirname"]; echo "<br/>"; print_r(pathinfo($path)); /*** * 相对路径 绝对路径 * 绝对路径的方法realpath */ $path1='demo6_1.php';//-相对路径 echo "<br/>"; echo realpath($path1); $path2='../index.php'; echo "<br/>"; echo realpath($path2); /*** * 磁盘 目录和文件的计算 * filesize 返回指定文件字节大小 * * * ---计算磁盘的可用空间 * ---计算磁盘的总容量 * ---文件的最后访问时间----时间戳 * ---文件最后改变时间 * ---文件最后修改时间。 */ echo "---------------<br/>"; $file ='/Applications/MAMP/htdocs/demo6/demo6_0.php'; echo round(filesize($file)/1024,2)."kb"; echo "-----drive----------<br/>"; //---计算磁盘的可用空间 $drive='/'; echo round(disk_free_space($drive)/1024/1024/1024,2)."G"; // * ---计算磁盘的总容量 echo "-----drive----------<br/>"; echo round(disk_total_space($drive)/1024/1024/1024,2)."G"; //* ---文件的最后访问时间 date_default_timezone_set('Asia/Shanghai'); echo "-----date----------<br/>"; echo date("Y-m-d,h:i:s"); echo "-----file----------<br/>"; echo date("Y-m-d,h:i:s",fileatime($drive)); //* ---文件的最后改变时间 echo "-----file----------<br/>"; echo date("Y-m-d,h:i:s",filectime($drive)); // * ---文件最后修改时间。 echo "-----file----------<br/>"; echo date("Y-m-d,h:i:s",filemtime($drive));?>
<?php header("Content-Type:text/html;charset=utf-8"); /*** * 文件处理。 * 资源类型---输入输出---- * 1.写入一个文件 * 打开文件 文件不存在 先创建 * 数据写入 * 关闭文件 * 2.读出数据 * 打开文件 * 读出数据 * 关闭文件 * * * */ /**** * 打开文件 * fopen()函数 支持文件路径 文件模式 * 文件模式--3中 * ->模式 * r只读:文件指针在文件头 * r+ 读写:.......文件头 * w 只写:在写入之前,删除文件内容,指针返回文件头 文件不存在 尝试创建。 * w+ 读写:读取和写入之前删除文件内容,移动到文件头 ,文件不存在。尝试创建 * a-只写:文件指针在文件的末尾,如果不存在创建,追加。 * a+读写 指针置于文件末尾,如果文件不存在,创建 追加到文件。 * b 二进制 是默认的模式,WIN系统区分二进制文件和文本文件。 * t 文本 * 1,只读 只写 读和写 * 2. 写一个文件 可能是覆盖所有已经有的文件内容,或者仅仅将数据追加到文件末尾。 * 3. 如果区分二进制和纯文本的系统还必须指定采用的方式。 * * * * fwrite 第三个是可选参数为最大字符数 * fputs * * * \n换行 */ //写入文件------- >追加 //写入文件------- > $fileone=fopen('file.txt','a'); //fopen返回的是资源类型resouce 资源句丙 //写入数据 fwrite($fileone,'张三的打算',strlen($fileone)); file_put_contents('file.txt',"thoifdojasfds"); fclose($fileone); //打开文件 $fp=fopen('file.txt','a'); $string ="内从\ncontent"; fwrite($fp,$string,strlen($fp)); fclose($fp); //读出文件------- > $fileone=fopen('file.txt','r'); /*** * fgetc()---读出一个字符 指针移动到下一个字符 * fgets()--读出一行字符 可以指定一行显示的长度 length-1 * fgetss()从文件指针中拂去一行并且过滤掉HTML的标记 * fread()读取定量的字符 length * fpassthru()输出文件指针处的所有的剩余数据 * file 将整个文件读入数组中 以行分组 * readfile()读入一个文件并写入到输出缓冲 所以不需要echo来输出 返回值是文件字符的总长度(字节数中文是2个) * file_get_contents()将整个文件读入一个字符串。 */ //读出一个字符 echo fgetc($fileone); echo fgetc($fileone); echo fgets($fileone,"3"); //过滤HTML echo fgetss($fileone); echo "<br/>"; //fread echo fread($fileone,4); echo "<br/>"; echo fpassthru($fileone); echo "<br/>"; print_r(file('file.txt')); echo readfile('file.txt'); echo file_get_contents('file.txt'); //打印所有的文件 函数feof();检测文件指针到了文件结尾的位置 while(!feof($fileone)) { echo fgetc($fileone); } //读取文件的时候一定保证这个文件一定操作。否则报错。不让执行 /*** * 文件是否存在 file_exists * 查看文件的大小 filesize * 删除一个文件 unlink * 在文件中定位 * * * * rewind 倒回文件的开始位置 * * * fseek 定位 */ echo "<br/>"; if(file_exists('file.txt')) { echo '存在'; rewind($fileone); echo fgetc($fileone); echo ftell($fileone); echo fseek($fileone,20); echo ftell($fileone); // unlink('file1.txt'); // filesize('file1.txt'); } else { echo '不存在'; } /*** * 锁定商品 * flock---锁定商品 * */ $ff=fopen('file.txt','ab'); flock($ff,LOCK_EX); fwrite($ff,'dsadas'); flock($ff,LOCK_UN); fclose($ff); /*** * 目录句丙 * opendir()打开目录 * closedir()关闭 * readdir()返回目录中的元素 * * * scandir 读入数组 * rmdir 删除目录 * rename 更改文件名 */ $dirs=opendir('/Applications/MAMP/htdocs/demo6'); while(!!$file ==readdir($dirs)) { echo $file.'<br/>'; } closedir($dir); print_r(scandir('/Applications/MAMP/htdocs/demo6')); rename('demo6_2.php','demo6_1.php'); ?>
PHP-第6节
最新推荐文章于 2024-06-10 19:14:04 发布