您的位置:首页 > 编程语言 > PHP开发

Php基础文件操作

2016-11-11 00:00 316 查看
1、获取文件名:

basename($filepath,$ext);$filepath 文件的全路径字符串 $ext 扩展名

<pre name="code" class="php">$path = "/home/httpd/html/index.php";
$file = basename($path,".php");
返回index


 2、获取文件路径的目录部分:

dirname($filepath) $filepath 文件的全路径字符串

<pre name="code" class="php">$path = "/home/httpd/html/index.php";
$file = dirname($path);
返回/home/httpd/html


 3、获取文件路径关联数组

pathinfo($filepath) $filepath 文件的全路径字符串

$pathinfo = pathinfo("/home/httpd/html/index.php");
var_dump($pathinfo);
返回
array(4) {  ["dirname"]=>  string(16) "/home/httpd/html"  ["basename"]=>  string(9) "index.php"  ["extension"]=>  string(3) "php"
["filename"]=>  string(5) "index"}/home/httpd/html

$pathinfo['dirname']//文件路径
$pathinfo['basename']//文件名称带扩展名
$pathinfo['extension']//扩展名
$pathinfo['filename']//文件名不带扩展名

4、取得文件的类型

filetype($filename) 返回文件的类型,可能的值有fifo、char、dir、block、link、file。unknown

5、获取指定文件有用信息数组

fstat() 获取由文件指针handle所打开文件的统计信息。包含的文件数组信息如下:

数字下标关联键名(自 PHP 4.0.6)说明
0dev设备名
1ino号码
2modeinode 保护模式
3nlink被连接数目
4uid所有者的用户 id
5gid所有者的组 id
6rdev设备类型,如果是 inode 设备的话
7size文件大小的字节数
8atime上次访问时间(Unix 时间戳)
9mtime上次修改时间(Unix 时间戳)
10ctime上次改变时间(Unix 时间戳)
11blksize文件系统 IO 的块大小
12blocks所占据块的数目
// 打开文件
$fp = fopen("index1.php", "r");
// 取得统计信息
$fstat = fstat($fp);
// 关闭文件
fclose($fp);
// 只显示关联数组部分
print_r(array_slice($fstat, 13));


stat($filename) 根据文件名获取文件信息数组,返回信息与fstat类似

注意:本函数的结果会被缓存。请使用 clearstatcache()来清除缓存;

6、目录操作:

遍历目录:

<?php
/**
*转载地址:http://blog.csdn.net/qiazi1983/article/details/7867381
*遍历目录,结果存入数组。支持php4及以上。php5以后可用scandir()函数代替while循环。
* @param string $dir
* @return array
*/
function my_scandir($dir)
{
$files = array();
if ( $handle = opendir($dir) ) {
while ( ($file = readdir($handle)) !== false )
{
if ( $file != ".." && $file != "." )
{
if ( is_dir($dir . "/" . $file) )
{
$files[$file] = my_scandir($dir . "/" . $file);
}
else
{
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}

function my_scandir1($dir)
{
$files = array();
$dir_list = scandir($dir);
foreach($dir_list as $file)
{
if ( $file != ".." && $file != "." )
{
if ( is_dir($dir . "/" . $file) )
{
$files[$file] = my_scandir1($dir . "/" . $file);
}
else
{
$files[] = $file;
}
}
}

return $files;
}

$result = my_scandir('./');
$result = my_scandir1('./');
?>
创建目录:

mkdir();

删除目录:

rmdir();

检查目录是否存在

is_dir();

7、操作文件:

新建文件:

$filename="test.txt";
$fp=fopen("$filename", "w+"); //打开文件指针,创建文件
if ( !is_writable($filename) ){
die("文件:" .$filename. "不可写,请检查!");
}
fclose($fp);  //关闭指针


读取文件:

if(is_readable("test.txt"))
{
<pre name="code" class="php">    //首先采用“fopen”函数打开文件,得到返回值的就是资源类型。
$file_handle = fopen("test.txt","r"); if ($file_handle){ //接着采用while循环(后面语言结构语句中的循环结构会详细介绍)一行行地读取文件,然后输出每行的文字 while (!feof($file_handle)) { //判断是否到最后一行 $line = fgets($file_handle); //读取一行文本 echo $line; //输出一行文本 echo "<br />"; //换行 } } fclose($file_handle);//关闭文件}else{ echo "文件不存在或者无法读取! ";}

在php5中可以采用 file_get_contents()方法,参数为:

参数描述
file必需。规定要写入数据的文件。如果文件不存在,则创建一个新文件。
flags可选。规定如何读取文件。可能的值:

FILE_USE_INCLUDE_PATH:使用包含目录定义

FILE_TEXT:文本格式内容。

FILE_BINARY:仅用于二进制格式。

如果参数省略则默认为FILE_BINARY
context上下文
offset可选。起始读位置,偏移。
maxlen可选。读入长度。
fopen 读写文件方式参数:

‘r’ 只读方式打开,将文件指针指向文件头。

‘r+’ 读写方式打开,将文件指针指向文件头。

‘w’ 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建文件。

‘w+’ 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建文件。

‘a’ 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建文件。

‘a+’ 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建文件。

 写入文件:

if(is_writable(test1.txt))
{
if(!$fso=fopen("test1.txt",'w')){
$this->warns('无法打开缓存文件.');//trigger_error
}
if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型锁定
$this->warns('无法锁定缓存文件.');//trigger_error
}
if(!fwrite($fso,"asdasdasdsadsadl了win笨笨按时打算大")){//写入字节流,serialize写入其他格式
$this->warns('无法写入缓存文件.');//trigger_error
}
flock($fso,LOCK_UN);//释放锁定
fclose($fso);
}
else
{
die("文件:" .$filename. "不可写,请检查!");
}
在PHP5中可采用file_put_contents()方法,参数如下:
<pre name="code" class="php">注意:要往文件里面写入大量的数据,则推荐用fwrite,不要用file_put_contents。在高并发的请求中也建议用fwrite。
就只有一句话,且不需要任何其他灵活的事情时使用。


复制文件:

$file = 'test1.txt';
$newfile = 'test2.txt'; # 这个文件父文件夹必须能写
if (file_exists($file) == false) {
die ('文件不存在无法复制');
}
$result = copy($file, $newfile);
if ($result == false) {
echo '复制成功';
}
删除文件:

unlink()方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: