PHP 第七周函数学习记录
trigger_error()
作用
trigger_error() 函数创建用户自定义的错误消息。
trigger_error() 函数用于在用户指定的条件下触发一个错误消息。它可以与内建的错误处理程序一起使用,或者与由 set_error_handler() 函数设置的用户自定义函数一起使用。
用法
trigger_error(error_message,error_types)
error_message 必需。规定错误消息。长度限制为 1024 个字符。
error_types 可选。规定错误消息的错误类型。
可能的错误类型:
E_USER_ERROR - 用户生成的运行时的致命错误。不能恢复的错误。停止执行脚本。
E_USER_WARNING - 用户生成的运行时的非致命警告。脚本没有停止执行。
E_USER_NOTICE - 默认。用户生成的运行时的通知。脚本发现可能是一个错误,但也可能在脚本正常运行时发生。
测试案例
<?php
$test=2;
if ($test>1)
{
trigger_error("A custom error has been triggered");
}
?>
结果
Notice: A custom error has been triggered
in C:webfoldertest.php on line 6
user_error()
作用
该函数是 trigger_error() 函数的别名。
basename()
作用
basename() 函数返回路径中的文件名部分。
用法
basename(path,suffix)
path 必需。规定要检查的路径。
suffix 可选。规定文件扩展名。如果文件有名有文件扩展名,将不会显示这个扩展名。
测试案例
<?php
$path = "/testweb/home.php";
//Show filename with file extension
echo basename($path) ."<br/>";
//Show filename without file extension
echo basename($path,".php");
?>
结果
home.php
home
chgrp()
作用
chgrp() 函数改变指定文件的用户组。
如果成功则返回 TRUE,如果失败则返回 FALSE。
用法
chgrp(file,group)
file 必需。规定要检查的文件。
group 可选。规定新的组。可以是组名或组的 ID。
测试案例
<?php
chgrp("test.txt","admin")
?>
chmod()
作用
chmod() 函数改变指定文件的权限。
如果成功则返回 TRUE,如果失败则返回 FALSE。
用法
chmod(file,mode)
file 必需。规定要检查的文件。
mode 必需。规定新的权限。
mode 参数由 4 个数字组成:=
第一个数字通常是 0
第二个数字规定所有者的权限
第三个数字规定所有者所属的用户组的权限
第四个数字规定其他所有人的权限
可能的值(如需设置多个权限,请对下面的数字进行总计):
1 = 执行权限
2 = 写权限
4 = 读权限
测试案例
<?php
// Read and write for owner, nothing for everybody else
chmod("test.txt",0600);
// Read and write for owner, read for everybody else
chmod("test.txt",0644);
// Everything for owner, read and execute for everybody else
chmod("test.txt",0755);
// Everything for owner, read for owner's group
chmod("test.txt",0740);
?>
chown()
作用
chown() 函数改变指定文件的所有者。
如果成功则返回 TRUE,如果失败则返回 FALSE。
用法
chown(file,owner)
file 必需。规定要检查的文件。
owner 必需。规定新的所有者。可以是用户名或用户的 ID。
测试案例
<?php
chown("test.txt","charles")
?>
clearstatcache()
作用
clearstatcache() 函数清除文件状态缓存。
PHP 会缓存某些函数的返回信息,以便提供更高的性能。但是有时候,比如在一个脚本中多次检查同一个文件,而该文件在此脚本执行期间有被删除或修改的危险时,你需要清除文件状态缓存,以便获得正确的结果。要做到这一点,请使用 clearstatcache() 函数。
用法
clearstatcache()
测试案例
<?php
//check filesize
echo filesize("test.txt");
echo "<br />";
$file = fopen("test.txt", "a+");
// truncate file
ftruncate($file,100);
fclose($file);
//Clear cache and check filesize again
clearstatcache();
echo filesize("test.txt");
?>
结果
792
100
copy()
作用
copy() 函数复制文件。
该函数如果成功则返回 TRUE,如果失败则返回 FALSE。
用法
copy(file,to_file)
测试案例
<?php
echo copy("source.txt","target.txt");
?>
结果
1
dirname()
作用
dirname() 函数返回路径中的目录名称部分。
用法
dirname(path)
测试案例
<?php
echo dirname("c:/testweb/home.php") . "<br />";
echo dirname("/testweb/home.php");
?>
结果
c:/testweb
/testweb
disk_free_space()
作用
disk_free_space() 函数返回指定目录的可用空间,以字节为单位。
用法
disk_free_space(directory)
测试案例
<?php
echo disk_free_space("C:");
?>
结果
109693288448
disk_total_space()
作用
disk_total_space() 函数返回指定目录的磁盘总容量,以字节为单位。
用法
disk_total_space(directory)
测试案例
<?php
echo disk_total_space("C:");
?>
结果
119990349824
diskfreespace()
作用
diskfreespace() 函数返回指定目录的可用空间,以字节为单位。
该函数是 disk_free_space() 函数的别名。
用法
diskfreespace(directory)
测试案例
<?php
echo diskfreespace("C:");
?>
结果
109693288448
fclose()
作用
fclose() 函数关闭打开的文件。
该函数如果成功则返回 TRUE,如果失败则返回 FALSE。
用法
fclose(file)
测试案例
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>
feof()
作用
feof() 函数检查是否已到达文件末尾(EOF)。
如果出错或者文件指针到了文件末尾(EOF)则返回 TRUE,否则返回 FALSE。
用法
feof(file)
测试案例
<?php
$file = fopen("test.txt", "r");
//Output a line of the file until the end is reached
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
结果
Hello, this is a test file.
There are three lines here.
This is the last line.
feof()
作用
feof() 函数检查是否已到达文件末尾(EOF)。
如果出错或者文件指针到了文件末尾(EOF)则返回 TRUE,否则返回 FALSE。
用法
feof(file)
测试案例
<?php
$file = fopen("test.txt", "r");
//Output a line of the file until the end is reached
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
结果
Hello, this is a test file.
There are three lines here.
This is the last line.
fflush()
作用
fflush() 函数向打开的文件写入所有的缓冲输出。
如果成功则返回 TRUE,如果失败则返回 FALSE。
用法
fflush(file)
测试案例
<?php
$file = fopen("test.txt","r+");
// some code
fflush($file);
?>
fgetc()
作用
fgetc() 函数从打开的文件中返回一个单一的字符。
注释:该函数处理大文件非常缓慢,所以它不用于处理大文件。如果您需要从一个大文件依次读取一个字符,请使用 fgets() 依次读取一行数据,然后使用 fgetc() 依次处理行数据。
用法
fgetc(file)
测试案例
<?php
$file = fopen("test2.txt","r");
echo fgetc($file);
fclose($file);
?>
结果
H
fgetcsv()
作用
fgetcsv() 函数从打开的文件中解析一行,校验 CSV 字段。
fgetcsv() 函数会在到达指定长度或读到文件末尾(EOF)时(以先到者为准),停止返回一个新行。
该函数如果成功则以数组形式返回 CSV 字段,如果失败或者到达文件末尾(EOF)则返回 FALSE。
用法
fgetcsv(file,length,separator,enclosure)
file 必需。规定要检查的文件。
length 可选。规定行的最大长度。必须大于 CSV 文件内最长的一行。如果忽略该参数(或者设置为 0),那么行长度就没有限制,不过可能会影响执行效率。
注意:该参数在 PHP 5 之前的版本是必需的。
separator 可选。设置字段分界符(只允许一个字符),默认值为逗号( , )。
enclosure 可选。设置字段环绕符(只允许一个字符),默认值为双引号( " )。
测试案例
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
CSV 文件:
Kai Jim, Refsnes, Stavanger, Norway
Hege, Refsnes, Stavanger, Norway
结果
Array
(
[0] => Kai Jim
[1] => Refsnes
[2] => Stavanger
[3] => Norway
)
测试案例
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
CSV 文件:
Kai Jim, Refsnes, Stavanger, Norway
Hege, Refsnes, Stavanger, Norway
结果
Array
(
[0] => Kai Jim
[1] => Refsnes
[2] => Stavanger
[3] => Norway
)
Array
(
[0] => Hege
[1] => Refsnes
[2] => Stavanger
[3] => Norway
)
fgets()
作用
fgets() 函数从打开的文件中返回一行。
fgets() 函数会在到达指定长度( length - 1 )、碰到换行符、读到文件末尾(EOF)时(以先到者为准),停止返回一个新行。
如果失败该函数返回 FALSE。
用法
fgets(file,length)
测试案例
<?php
$file = fopen("test.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
结果
Hello, this is a test file.
There are three lines here.
This is the last line.
fgetss()
作用
fgetss() 函数从打开的文件中返回一行,并过滤掉 HTML 和 PHP 标签。
fgetss() 函数会在到达指定长度或读到文件末尾(EOF)时(以先到者为准),停止返回一个新行。
如果失败该函数返回 FALSE。
用法
fgetss(file,length,tags)
测试案例
test.html 代码内容:
<p><b>This is a paragraph.</b></p>
PHP 代码:
<?php
$file = fopen("test.html","r");
echo fgetss($file);
fclose($file);
?>
结果
This is a paragraph.
file()
作用
file() 函数把整个文件读入一个数组中。
数组中的每个元素都是文件中相应的一行,包括换行符在内。
用法
file(path,include_path,context)
path 必需。规定要读取的文件。
include_path 可选。如果您还想在 include_path(在 php.ini 中)中搜索文件的话,请设置该参数为 '1'。
context 可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。若使用 NULL,则忽略。
测试案例
<?php
print_r(file("test.txt"));
?>
结果
Array
(
[0] => Hello World. Testing testing!
[1] => Another day, another line.
[2] => If the array picks up this line,
[3] => then is it a pickup line?
)
file_exists()
作用
file_exists() 函数检查文件或目录是否存在。
如果指定的文件或目录存在则返回 TRUE,否则返回 FALSE。
用法
file_exists(path)
测试案例
<?php
echo file_exists("test.txt");
?>
结果
1
file_get_contents()
作用
file_get_contents() 把整个文件读入一个字符串中。
该函数是用于把文件的内容读入到一个字符串中的首选方法。如果服务器操作系统支持,还会使用内存映射技术来增强性能。
用法
file_get_contents(path,include_path,context,start,max_length)
path 必需。规定要读取的文件。
include_path 可选。如果您还想在 include_path(在 php.ini 中)中搜索文件的话,请设置该参数为 '1'。
context 可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。若使用 NULL,则忽略。
start 可选。规定在文件中开始读取的位置。该参数是 PHP 5.1 中新增的。
max_length 可选。规定读取的字节数。该参数是 PHP 5.1 中新增的。
测试案例
<?php
echo file_get_contents("test.txt");
?>
结果
This is a test file with test text.
file_put_contents()
作用
file_put_contents() 函数把一个字符串写入文件中。
该函数访问文件时,遵循以下规则:
如果设置了 FILE_USE_INCLUDE_PATH,那么将检查 *filename* 副本的内置路径
如果文件不存在,将创建一个文件
打开文件
如果设置了 LOCK_EX,那么将锁定文件
如果设置了 FILE_APPEND,那么将移至文件末尾。否则,将会清除文件的内容
向文件中写入数据
关闭文件并对所有文件解锁
如果成功,该函数将返回写入文件中的字符数。如果失败,则返回 False。
用法
int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
file 必需。规定要写入数据的文件。如果文件不存在,则创建一个新文件。
data 必需。规定要写入文件的数据。可以是字符串、数组或数据流。
mode 可选。规定如何打开/写入文件。可能的值:
FILE_USE_INCLUDE_PATH
FILE_APPEND
LOCK_EX
context 可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。
测试案例
<?php
echo file_put_contents("sites.txt","Runoob");
?>
结果
6
fileatime()
作用
fileatime() 函数返回指定文件的上次访问时间。
如果成功,该函数将以 Unix 时间戳形式返回文件的上次访问时间。如果失败,则返回 FALSE。
用法
fileatime(filename)
测试案例
<?php
echo fileatime("test.txt");
echo "<br />";
echo "Last access: ".date("F d Y H:i:s.",fileatime("test.txt"));
?>
结果
1140684501
Last access: February 23 2006 09:48:21.
filectime()
作用
filectime() 函数返回指定文件的上次修改时间。
该函数将检查文件的日常修改情况和 inode 修改情况。inode 修改情况是指:权限的修改、所有者的修改、用户组的修改或其他元数据的修改。
如果成功,该函数将以 Unix 时间戳形式返回文件的上次修改时间。如果失败,则返回 FALSE。
注释:该函数的结果会被缓存。请使用 clearstatcache() 来清除缓存。
用法
filectime(filename)
测试案例
<?php
echo filectime("test.txt");
echo "<br />";
echo "Last change: ".date("F d Y H:i:s.",filectime("test.txt"));
?>
结果
1138609592
Last change: January 30 2006 09:26:32.
filegroup()
作用
filegroup() 函数返回指定文件的组 ID。
如果成功,该函数返回指定文件所属组的 ID。如果失败,则返回 FALSE。
用法
filegroup(filename)
测试案例
<?php
echo filegroup("test.txt");
?>
参考
本作品采用《CC 协议》,转载必须注明作者和本文链接