PHP文件处理详解

将相关的数据组织成文件与目录的形式,是现代计算机的核心概念。所以,要获得文件信息或对其修改需要如下函数:

<?php
/**
 * 获取文件的文件名
 * basename(string path [, string suffix])
 */
$path = "./o.txt";
print_r(basename($path)); // o.txt
// 忽略后缀名
printf("Filename sans extension: %s", basename($path, '.txt')); // Filename sans extension: o


/**
 * 获取路径目录
 * dirname(string path)
 */
$path = "/home/users.txt";
print_r("Directory path: ". $path); // Directory path: /home/users.txt


/**
 * 了解更多关于路径信息
 * pathinfo(string path [, options])
 */
$pathinfo = pathinfo("/home/www/htdocs/book/index.html");
print_r($pathinfo);
/*Array
(
    [dirname] => /home/www/htdocs/book
    [basename] => index.html
    [extension] => html
    [filename] => index
)*/
// 返回指定数据
$pathinfo = pathinfo("/home/www/htdocs/book/index.html", PATHINFO_FILENAME); // index


/**
 * 确定绝对路径
 * realpath(string path)
 */
$textpath = realpath("./o.txt");
echo $textpath;
/* E:\work\app\phptest\o.txt */


/**
 * 确定文件大小
 * filesize(string filename)
 */
$file = realpath("./php.ini");
$bytes = filesize($file);
$kilobytes = round($bytes / 1024, 2);
printf("File %s is $bytes bytes, or %.2f kilobytes", basename($file), $kilobytes);
/* File php.ini is 72999 bytes, or 71.29 kilobytes */

/**
 * 计算磁盘的可用空间
 * disk_free_space(string directory)
 */
$drive = "E:";
printf("Remaining MB on %s %.2f", $drive, round(disk_free_space($drive) / 1024 ** 2, 2));
/* Remaining MB on E: 663196.99 */

echo "\r";

/**
 * 计算磁盘总空间
 * disk_total_space(string directory)
 */
$drive = "E:";
$totalSpace = disk_total_space($drive) / 1024 ** 2;
$userSpace = $totalSpace - disk_free_space($drive) / 1024 ** 2;
printf("Partition: %s (Allocated: %.2f MB. Used: %.2f MB. )", $drive, $totalSpace, $userSpace);
/* Partition: E: (Allocated: 953867.00 MB. Used: 290670.02 MB. ) */

echo "\r";

/**
 * 确定目录内容大小
 */
function directorySize($directory)
{
	$directorySize = 0;

	if ($dh = opendir($directory))
	{
		// 迭代处理每个目录项
		while (($filename = readdir($dh)))
		{
			if ($filename != "." && $filename != "..")
			{
				$newFilename = $directory . "/" . $filename;

				// 确定大小并添加到总大小
				if (is_file($newFilename))
					$directorySize += filesize($newFilename);

				// 新目录递归
				if (is_dir($newFilename))
					$directorySize += directorySize($newFilename);
			}
		}
	}

	closedir($dh);
	return $directorySize;
}

$directory = 'E:/work/app/phptest';

$totalSize = round((directorySize($directory) / 1024 ** 2), 2);
printf("Directory %s: %f MB", $directory, $totalSize); 
/* Directory E:/work/app/phptest: 0.080000 MB */

echo "\r";

/**
 * 确定文件的最后访问时间
 * fileatime(string filename)
 */
$file = './ext/php.ini';
printf("File last accessed: %s", date("m-d-y g:i:sa", fileatime($file)));
/* File last accessed: 08-23-19 4:57:58am */

echo "\r";

/**
 * 确定文件的最后改变时间(不等于最后修改时间,这个表示的inode数据的任何改变,包括权限、所有者、组等)
 * filectime(string filename)
 */
$file = './ext/php.ini';
printf("File inode last changed: %s", date("m-d-y g:i:sa", filectime($file)));
/* File inode last changed: 08-16-19 10:55:38am */

echo "\r";

/**
 * 确定文件的最后修改时间(关于内容修改)
 * filemtime(string filename)
 */
$file = './ext/php.ini';
printf("File last updated: %s", date("m-d-y g:i:sa", filemtime($file)));
/* File last updated: 08-23-19 4:57:58am */

echo "\r";

/**
 * 打开和关闭文件
 * fopen(string resource, string mode [, int use_include_path [, resource context]])
 * 其中use_include_path传1则使用配置指令include_path
 * mode:
 * R 只读,文件指针至于文件开头
 * r+ 读写,文件指针至于问价开头
 * W 只写,删除文件内容,指针文件开头,文件不存在则创建
 * w+ 读写,同上
 * A 只写,文件指针末尾,文件不存在穿件。此模式称为追加
 * a+ 读写,同上
 * x 创建并打开只写的文件,文件不存在报错
 * x+ 创建并以读写的方式打开文件,同上
 */
$file = './o.txt';
$fh = fopen($file, 'r');

// 关闭文件指针
fclose($fh);
/* File last updated: 08-23-19 4:57:58am */

echo "\r";

/**
 * 读取文件
 * file(string filename [int use_include_path[, resource context]])
 */
$user = file("o.txt"); 
print_r($user); // 返回array,按换行分隔数组

echo "\r";

/**
 * 将文件内容读入字符串变量
 * file_get_contents(string filename[, int use_include_path [, resource context, [int offset [, int maxlen]]]])
 */
$user = file_get_contents("o.txt"); 
print_r($user); // 返回string

echo "\r";

/**
 * 写入文件
 * fwrite(handle, string, [length])
 */
$subscriberInfo = "Jason Gilmore | xxx@xxx.com";
$fh = fopen("test.txt", 'a');
fwrite($fh, $subscriberInfo);
fclose($fh);

/**
 * 写入文件
 * file_put_contents(file, data, [mode, context])
 * file 文件地址
 * data 输入数据
 * mode 方式
 *  - FILE_USE_INCLUDE_PATH (同上)
 *  - FILE_APPEND (追加)
 *  - LOCK_EX (加锁)
 *  多个用|隔开
 * context 规定文件句柄环境
 */
$subscriberInfo = "Jason Gilmore | xxx@xxx.com";
file_put_contents("test.txt", $subscriberInfo, FILE_APPEND | LOCK_EX);

echo "\r";

/**
 * 目录相关
 * 打开目录 opendir(path, [context])
 * 关闭目录 closedir(resource)
 * 解析目录内容 readdir(resource)
 * 写入数组 scandir(directory, [sorting_order, context])
 */
// 输出所有文件和子目录
$dh = opendir('../phptest');
while ($file = readdir($dh))
	echo "$file \r";
closedir($dh);

print_r(scandir('../phptest')); // array
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值