PHP文件编程的介绍

不言
Lepaskan: 2023-04-02 13:28:02
asal
2118 orang telah melayarinya

这篇文章主要介绍了关于PHP文件编程的介绍,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

1 获取文件信息

1.1 第一种方式(fopen、fstat、file_exists)

<?php
$file_full_path = &#39;./test.txt&#39;;
if(file_exists($file_full_path)){		// 检查文件或目录是否存在,存在则返回 TRUE,否则返回 FALSE
	$fp = fopen($file_full_path, &#39;r&#39;);	// 打开文件或url,成功时返回文件指针资源,如果打开失败,本函数返回 FALSE。
	$fileinfo_arr = fstat($fp);			// 通过已打开的文件指针取得文件信息,返回一个数组具有该文件的统计信息

	echo &#39;<pre class="brush:php;toolbar:false">&#39;;
	var_dump($fileinfo_arr);

	echo &#39;文件的大小是:&#39; . $fileinfo_arr[&#39;size&#39;] . &#39;个字节&#39;;
	echo &#39;文件的创建时间是:&#39; . date(&#39;Y-m-d H:i:s&#39;, $fileinfo_arr[&#39;ctime&#39;]);
	echo &#39;文件的访问时间是:&#39; . date(&#39;Y-m-d H:i:s&#39;, $fileinfo_arr[&#39;atime&#39;]);
	echo &#39;文件的修改时间是:&#39; . date(&#39;Y-m-d H:i:s&#39;, $fileinfo_arr[&#39;mtime&#39;]);
}else{
	echo &#39;文件不存在&#39;;
}
Salin selepas log masuk

1.2 第二种方式

<?php
$file_full_path = &#39;./test.txt&#39;;
if(file_exists($file_full_path)){
	echo &#39;文件的大小是:&#39; . filesize($file_full_path);
	echo &#39;文件的类型是:&#39; . filetype($file_full_path);

	echo &#39;文件的创建时间是:&#39; . date(&#39;Y-m-d H:i:s&#39;, filectime($file_full_path));
	echo &#39;文件的访问时间是:&#39; . date(&#39;Y-m-d H:i:s&#39;, fileatime($file_full_path));
	echo &#39;文件的修改时间是:&#39; . date(&#39;Y-m-d H:i:s&#39;, filemtime($file_full_path));
}else{
	echo &#39;文件不存在&#39;;
}
Salin selepas log masuk

2 读取文件内容

2.1 第一种方式,fread

<?php
$file_full_path = &#39;./test.txt&#39;;
if(file_exists($file_full_path)){
	// 1、打开文件
	$fp = fopen($file_full_path, &#39;r&#39;);
	// 2、获取文件的大小
	$file_size = filesize($file_full_path);
	// 3、读取内容
	$con_str = fread($fp, $file_size);		// 返回所读取的字符串, 或者在失败时返回 FALSE。
	fclose($fp);
	// 替换换行符
	$con_str = str_replace("\r\n", &#39;<br>&#39;, $con_str);
	$con_str = str_replace("\n", &#39;<br>&#39;, $con_str);
	// 替换 tab
	$con_str = str_replace("	", "    ", $con_str);

	echo $con_str;

}else{
	echo &#39;文件不存在&#39;;
}
Salin selepas log masuk

2.2 第二种方式,feof

<?php
$file_full_path = &#39;./test.txt&#39;;
if(file_exists($file_full_path)){
	$fp = fopen($file_full_path, &#39;r&#39;);
	// 设置缓冲
	$buffer = &#39;&#39;;
	$buffer_size = 1024;
	$con_str = &#39;&#39;;

	while(!feof($fp)){				// 测试文件指针是否到了文件结束的位置,到达返回true,否则返回false
		$buffer = fread($fp, $buffer_size);
		$con_str .= $buffer;
	}

	// 关闭文件
	fclose($fp);
	$con_str = str_replace("\r\n", &#39;<br>&#39;, $con_str);
	$con_str = str_replace("\n", &#39;<br>&#39;, $con_str);
	$con_str = str_replace("	", &#39;    &#39;, $con_str);
	echo $con_str;
}else{
	echo &#39;文件不存在&#39;;
}
Salin selepas log masuk

2.3 第三种方式,file_get_contents

<?php
$file_full_path = &#39;./test.txt&#39;;
if(file_exists($file_full_path)){
	$con_str = file_get_contents($file_full_path);		//  将整个文件读入一个字符串

	$con_str = str_replace("\r\n", &#39;<br>&#39;, $con_str);
	$con_str = str_replace("\n", &#39;<br>&#39;, $con_str);
	$con_str = str_replace("	", &#39;    &#39;, $con_str);
	
	echo $con_str;
}else{
	echo &#39;文件不存在&#39;;
}
Salin selepas log masuk

3 创建文件,并写入内容

3.1 案例1

<?php
$file_full_path = &#39;./test.txt&#39;;
if(!file_exists($file_full_path)){
	if($fp = fopen($file_full_path, &#39;w&#39;)){		// 覆盖写入10句helloworld
		$con = &#39;&#39;;
		for($i=0; $i<10; $i++){
			$con .= "HelloWorld\r\n";
		}

		// 写入文件
		fwrite($fp, $con);			// fwrite() 返回写入的字符数,出现错误时则返回 FALSE 。 
		fclose($fp);
	}else{
		echo &#39;创建文件失败&#39;;
	}
}else{
	echo &#39;文件已经存在&#39;;
}
Salin selepas log masuk

3.2 案例2,file_put_contents

<?php
$file_full_path = &#39;./test.txt&#39;;
if(!file_exists($file_full_path)){
	$con = &#39;&#39;;
	for($i=0; $i<10; $i++){
		$con .= "helloworld\r\n";
	}
	// 默认是覆盖写,可以追加FILE_APPEND参数,改为追加写。
	file_put_contents($file_full_path, $con);		// 和依次调用 fopen(),fwrite() 以及 fclose() 功能一样。 
}else{
	echo &#39;已经存在该文件&#39;;
}
Salin selepas log masuk

4 删除文件

<?php
$file_full_path = &#39;./test.txt&#39;;
if(file_exists($file_full_path)){
	if(unlink($file_full_path)){
		echo &#39;<br>删除成功&#39;;
	}else{
		echo &#39;<br>删除失败&#39;;
	}
}else{
	echo &#39;文件不存在,无法删除&#39;;
}
Salin selepas log masuk

5 修改文件名

<?php
$file_full_path = &#39;./test.txt&#39;;
$file_new_full_path = &#39;./王八.txt&#39;;
$file_new_full_path = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $file_new_full_path);
if(file_exists($file_full_path)){
	if(rename($file_full_path, $file_new_full_path)){			// 重命名一个文件或目录
		echo &#39;改名成功!&#39;;
	}else{
		echo &#39;改名失败!&#39;;
	}
}else{
	echo &#39;文件不存在&#39;;
}
Salin selepas log masuk

6 操作文件目录

6.1 创建一级目录

<?php
$dir_full_path = &#39;./abc&#39;;

// 判断有没有该目录
if(!is_dir($dir_full_path)){
	if(mkdir($dir_full_path)){
		echo &#39;创建目录成功!&#39;;
	}else{
		echo &#39;创建目录失败!&#39;;
	}
}else{
	echo &#39;已经存在该目录,无法再次创建&#39;;
}
Salin selepas log masuk

6.2 创建多级目录

<?php
$dir_full_path = &#39;./abc/edf/xyz&#39;;
if(!is_dir($dir_full_path)){
	if(mkdir($dir_full_path, 0777, true)){		// true 表示递归创建
		echo &#39;创建目录成功&#39;;
	}else{
		echo &#39;创建目录失败&#39;;
	}
}else{
	echo &#39;已经存在该目录,无法再次创建!&#39;;
}
Salin selepas log masuk

6.3 删除目录(一级)

<?php
$dir_full_path = &#39;./abc&#39;;
if(is_dir($dir_full_path)){
	if(rmdir($dir_full_path)){
		echo &#39;删除目录成功&#39;;
	}else{
		echo &#39;删除目录失败&#39;;
	}
}else{
	echo &#39;不存在该文件夹&#39;;
}
Salin selepas log masuk

7 文件编程的应用案例

7.1 如何拷贝一张图片

<?php
$file_src_full_path = &#39;F:/壁纸.jpg&#39;;
$file_src_full_path = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $file_src_full_path);

$file_des_full_path = &#39;D:/amp/WWW/萧山.jpg&#39;;
$file_des_full_path = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $file_des_full_path);

if(file_exists($file_src_full_path)){
	if(copy($file_src_full_path, $file_des_full_path)){
		echo &#39;拷贝成功&#39;;
	}else{
		echo &#39;拷贝失败&#39;;
	}
}else{
	echo &#39;没有这个文件&#39;;
}
Salin selepas log masuk

7.2 遍历某个文件夹,判断文件夹下面内容分别是目录和文件

<?php
$dir_full_path = &#39;D:/amp/WWW/&#39;;
if(is_dir($dir_full_path)){
	$dir_handle = opendir($dir_full_path);		// 如果成功则返回目录句柄的 resource,失败则返回 FALSE
	while(($file_name = readdir($dir_handle)) !== false){		// 成功则返回文件名 或者在失败时返回 FALSE 
		if(is_dir($dir_full_path . $file_name)){
			echo $file_name . &#39;是目录<br>&#39;;
		}else{
			echo $file_name . &#39;是文件<br>&#39;;
		}
	}

	closedir($dir_handle);
}else{
	echo &#39;不是目录,无法打开&#39;;
}
Salin selepas log masuk

7.3 写一个函数统计某个目录所有文件的大小

<?php
$dir_name = &#39;D:/amp/WWW&#39;;
function getDirSize($dir_name){
	$dir_size = 0;
	$dir_handle = opendir($dir_name);
	while(($file_name = readdir($dir_handle)) !== false){
		$file = $dir_name . &#39;/&#39; . $file_name;		// 文件全名
		if($file_name!==&#39;.&#39; && $file_name!==&#39;..&#39;){
			if(is_dir($file)){
				$dir_size += getDirSize($file);
			}else{
				$dir_size += filesize($file);
			}
		}
	}
	closedir($dir_handle);
	return $dir_size;
}

echo getDirSize($dir_name);
Salin selepas log masuk
<br/>
Salin selepas log masuk

7.4 删除某个目录

<?php
$dir_name = &#39;D:/amp/WWW/.idea&#39;;
function rrmdir($src){
	$dir_handle = opendir($src);
	while(false !== ($file = readdir($dir_handle))){
		if(($file != &#39;.&#39;) && ($file != &#39;..&#39;)){
			$full = $src . &#39;/&#39; . $file;
			if(is_dir($full)){
				rrmdir($full);
			}else{
				unlink($full);
			}
		}
	}
	closedir($dir_handle);
	rmdir($src);
}
rrmdir($dir_name);
Salin selepas log masuk

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网! 

 相关推荐:

php 遍历一个文件夹下的所有文件和子文件的代码

PHP的文件和目录操作

Atas ialah kandungan terperinci PHP文件编程的介绍. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!