PHP 文件相关函数

本文介绍了 PHP 文件的相关函数。本文只是部分列举,详情请查看官方文档。

读取文件最后一行

1
2
3
4
5
6
7
8
9
10
11
12
13
$pos = -2;		//偏移量
$eof = " "; //行尾标识
$data = "";
while ($line > 0){//逐行遍历
while ($eof != "\n"){ //不是行尾
fseek($fp, $pos, SEEK_END);//fseek成功返回0,失败返回-1
$eof = fgetc($fp);//读取一个字符并赋给行尾标识
$pos--;//向前偏移
}
$eof = " ";
$data .= fgets($fp);//读取一行
$line--;
}

HTML 上传文件

1
2
3
4
<form action="url" method="post" enctype="multipart/form-data">
<input type="file" name="name" id='file' />
<input type="submit" />
</form>

PHP 会将文件存到临时文件中,脚本结束就会销毁,所以必须存到另外的位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$file = $_FILE['file'];

// 文件名
$file['name'];

// 类型
$file['type'];

// 临时文件名
$file['tmp_name'];

// error
$file['error'];

// size
$file['size'];

// 保存文件
move_uploaded_file($_FILES['file']['tmp_name'], '/path/filename');

获取路径

  • 执行命令所在路径 getcwd()

  • 文件所在路径 __FILE__

  • 文件所在目录路径 __DIR__ dirname(__FILE__)

目录相关函数

1
resource opendir( string $path [, resource $context ] )

打开一个目录句柄,可用于之后的 closedir() readdir() rewinddir()

1
2
3
4
5
6
7
8
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
  • readdir() 从目录句柄中读取条目。

  • chdir() 改变目录

  • chroot() 改变根目录

  • closedir() 关闭目录句柄

  • dir() 返回一个 Directory 类实例

1
2
3
4
5
6
7
$d=dir($path);

$d->handle;

$entry=$d->read();

$d->close();
  • getcwd() 取得当前工作目录

  • rewinddir() 倒回目录句柄

  • scandir() 列出指定路径中的文件和目录

1
array scandir ( string $directory [, int $sorting_order [, resource $context ]] )

返回包含文件和目录的数组。

SPL

SplFileInfo

1
2
3
4
5
$file = SplFileInfo('file');

$file->getExtension();

$file->getRealPath();

SplFileObject

1
2
3
4
5
$file = new SplFileObject($file_name [, $open_mode, $use_include_path, $context])

$file->eof();

$file->fgets();

文件操作相关函数

  • 更改权限

    chgrp() chmod() chown()

  • 复制

    copy()

  • 删除

    unlink() unset()

  • 判断

    is_dir() is_file() is_executable() is_link()

读写文件

官方文档:http://php.net/manual/zh/function.file-get-contents.php

file_get_contents($filePath)

file_put_contents($filePath, $content) 可以通过设置第三个参数,将内容追加,而不是覆盖。

官方文档:http://php.net/manual/zh/function.stream-get-contents.php

stream_get_contents($resource) 读取资源流到一个字符串

stream_context_create($array) 创建资源流上下文,参数必须为 关联数组

官方文档:http://php.net/manual/zh/function.fopen.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$fh = fopen($filePath, $mode);

// 返回类型为资源

fgetc($fp); // 读取一个字符

fgets($fh); // 读取一行

fread($fh, $length);

fwrite($fh, $string);

fclose($fh);

file($filePath); // 返回结果为数组,一行一个值
0%