PHP文件编程

1 获得文件信息

1.1 第一种方式

<?php
$file_path = './test.txt';

$handle = fopen($file_path, 'r');		// 打开文件或者 URL
$file_info_arr = fstat($handle);		// 通过已打开的文件指针取得文件信息,返回一个数组
echo '文件的大小为:' . $file_info_arr['size'] . '<br>';
echo '文件的创建时间为:' . date('Y-m-d H:i:s', $file_info_arr['ctime']) . '<br>';
echo '文件的访问时间为:' . date('Y-m-d H:i:s', $file_info_arr['atime']) . '<br>';
echo '文件的修改时间为:' . date('Y-m-d H:i:s', $file_info_arr['mtime']) . '<br>';

1.2 第二种方式

<?php
$file_path = './test.txt';

if(file_exists($file_path)){
	echo '文件的大小为:' . filesize($file_path) . '<br>';
	echo '文件的类型为:' . filetype($file_path) . '<br>';
	echo '文件的创建时间为:' . date('Y-m-d H:i:s', filectime($file_path)) . '<br>';
	echo '文件的访问时间为:' . date('Y-m-d H:i:s', fileatime($file_path)) . '<br>';
	echo '文件的修改时间为:' . date('Y-m-d H:i:s', filemtime($file_path)) . '<br>';
}else{
	echo '文件不存在或文件路径有误';
}

2 读取文件的四种方式

2.1 第一种方式

<?php
$file_path = './test.txt';
// 一次性读取文件所有内容
if(file_exists($file_path)){
	$handle = fopen($file_path, 'r');
	$file_size = filesize($file_path);
	if($file_size != 0){
		$file_content = fread($handle, $file_size);		// 读取文件
		echo $file_content;
	}else{
		echo '对不起,文件的大小为0,没有内容';
	}
	fclose($handle);
}else{
	echo '文件不存在或文件的信息有误';
}

2.2 第二种方式

<?php
$file_path = './test.txt';
if(file_exists($file_path)){
	$handle = fopen($file_path, 'r');
	$file_size = filesize($file_path);

	$buffer = 512;									// 设置缓冲大小
	$file_content = '';
	while(!feof($handle)){
		$file_content .= fread($handle, $buffer);
	}

	echo $file_content;
	fclose($handle);
}else{
	echo '文件不存在或路径有误';
}

2.3 第三种方式

$file_path = './test.txt';
if(file_exists($file_path)){
	$file_content = file_get_contents($file_path);
	echo $file_content;
}else{
	echo '文件不存在,或路径有误';
}

2.4 第四种方式

<?php
$file_path = 'test.ini';
if(file_exists($file_path)){
	$file_content_arr = parse_ini_file($file_path);
	var_dump($file_content_arr);
}else{
	echo '文件不存在或文件路径有误';
}

3 写入内容

3.1 第一种方式fwrite()

<?php
$file_path = 'test.txt';
$handle = fopen($file_path, 'a+');
fwrite($handle, 'ABC! ENGLISH');
fclose($handle);
echo '写入成功';

3.2 第二种方式file_put_contents()

<?php
$file_path = 'message.txt';
$result = file_put_contents($file_path, '哥们儿,手头紧吗?', FILE_APPEND);		// 第三个参数默认是覆盖写,FILE_APPEND是追加写

4 重命名文件

<?php
$file_path = 'message.txt';
$new_file_path = '留言板.txt';
$new_file_path = iconv('utf-8', 'gbk', $new_file_path);
if(file_exists($file_path)){
	if(rename($file_path, $new_file_path)){
		echo '文件重命名成功!';
	}else{
		echo '文件重命名失败!';
	}	
}else{
	echo '文件不存在!';
}

5 拷贝文件

<?php
$file_path = 'message.txt';
if(file_exists($file_path)){
	$new_file_path = 'D:/www/留言板.txt';
	$new_file_path = iconv('utf-8', 'gbk', $new_file_path);

	if(copy($file_path, $new_file_path)){
		echo '拷贝成功';
	}else{
		echo '拷贝失败';
	}
}else{
	echo '文件不存在';
}

6 删除文件

<?php
$file_path = 'message.txt';
if(file_exists($file_path)){
	if(unlink($file_path)){		// 删除文件
		echo '删除成功!';
	}else{
		echo '删除失败!';
	}
}else{
	echo '文件不存在';
}

7 创建目录

<?php
$dir = 'D:/test';
if(!is_dir($dir)){
	if(mkdir($dir)){
		echo '创建目录成功';
	}else{
		echo '创建目录失败';
	}
}else{
	echo '目录已经存在,无需创建!';
}

如果我们要创建子目录的话,需要借助mkdir的后面的两个参数

<?php
$date = date('Ymd');
$dir = 'D:/test/demo' . $date;
if(!is_dir($dir)){
	if(mkdir($dir, 0777, true)){
		echo '目录创建成功!';
	}else{
		echo '目录创建失败!';
	}
}else{
	echo '目录已经存在,无需创建!';
}

8 删除目录

<?php
$date = date('Ymd');
$dir = 'D:/test/demo/' . $date;
if(is_dir($dir)){
	if(rmdir($dir)){
		echo '删除目录成功!';
	}else{
		echo '删除目录失败!';
	}
}else{
	echo '不是一个目录或目录不存在,无法删除';
}

9 遍历目录

<?php
$dir = 'D:/test';
if(is_dir($dir)){
	$dir_handle = opendir($dir);	// 打开目录句柄
	while($file = readdir($dir_handle)){		// 从目录句柄中读取条目, 成功则返回文件名 或者在失败时返回 FALSE 
		echo $file . '<br>';
	}
	closedir($dir_handle);			// 关闭目录句柄
}else{
	echo '不是目录';
}

10 自定义一个函数,实现parse_ini_file()函数功能

 

代码如下:

<?php
function my_parse_ini_file($file_path){
	// 判断文件是否存在
	if(file_exists($file_path)){
		$file_handle = fopen($file_path, 'r');

		while($line = fgets($file_handle)){
			$line = str_replace('', '', $line);
			$line = str_replace("
", '', $line);

			$arr = explode('=', $line);
			$result[$arr[0]] = $arr[1];
		}
		return $result;
	}else{
		echo '文件不存在,或文件的路径有误';
	}
}

$result = my_parse_ini_file('test.ini');
echo '<pre>';
var_dump($result);

结果如下:

array(2) {
  ["load"]=>
  string(7) "loading"
  ["li"]=>
  string(5) "zhang"
}

11 编写一个程序,具体要求如下图:

image

<?php
$year = date('Y');
$month = date('md');

$full_dir = 'D:/' . $year . '/' . $month;
if(!is_dir($full_dir)){
	mkdir($full_dir, 0777, true);
}

$file_path = $full_dir . '/abc.txt';
$file_handle = fopen($file_path, 'w+');

$content = '';
for($i=0; $i<100; $i++){
	$content .= 'helloworld' . "
";
}

fwrite($file_handle, $content);
fclose($file_handle);
echo '完成操作';

12 遍历某个文件夹下的所有的文件和目录(使用递归)

<?php
$dir = 'D:/2018';
function bianLi($dir){
	if(is_dir($dir)){
		$dir_handle = opendir($dir);
		while($file = readdir($dir_handle)){
			if($file == '.' || $file == '..'){
				continue;
			}
			if(is_dir($dir . '/' . $file)){
				bianLi($dir . '/' . $file);
			}
			echo $file . '------------' . filetype($dir . '/' . $file) . '<br>';
		}
	}else{
		echo '不是一个目录,无法遍历';
		exit();
	}
}

bianLi($dir);

13 统计某个目录所有文件的大小(使用递归)

<?php
$dir = 'D:/2018';
function getDirSize($dir){
	if(is_dir($dir)){
		$dir_handle = opendir($dir);
		static $size = 0;
		while(false !== ($file=readdir($dir_handle))){
			if($file == '.' || $file == '..'){
				continue;
			}
			if(!is_dir($dir . '/' . $file)){
				$size += filesize($dir . '/' . $file);
			}else{
				getDirSize($dir . '/' . $file);
			}
		}
	}else{
		$size = filesize($dir);
	}
	return $size;
}

$size = getDirSize($dir);
echo $size;

14 删除某个目录(使用递归)

<?php
$dir = 'D:/2018';
function removeDir($dir){
	if(is_dir($dir)){
		$dir_handle = opendir($dir);		// 打开目录句柄
		while(false !== ($file=readdir($dir_handle))){		// 从目录句柄中读取条目
			if($file == '.' || $file == '..'){
				continue;
			}
			if(is_dir($dir . '/' . $file)){
				removeDir($dir . '/' . $file);
			}else{
				unlink($dir . '/' . $file);		// 删除文件
			}
		}
		closedir($dir_handle);		// 关闭目录句柄
		rmdir($dir . '/' . $file);	// 删除目录
	}else{
		unlink($dir);		// 删除文件
	}
}

removeDir($dir);
echo '删除成功!';
 
原文地址:https://www.cnblogs.com/falling-maple/p/9264817.html