php Filesystem

file----把整个文件读入数组中

description:

array file(string $filename [, use $use_include_path[,resource $context]])

和get_file_contents()一样,只除了file()将文件作为一个数组返回,数组中每个单元都是文件中的一行,包括换行符在内,如果失败返回false

如果想在include_path中查找的话,只需将$use_include_path设为1

examples

<?php
// 将一个文件读入数组。本例中通过 HTTP 从 URL 中取得 HTML 源文件。
$lines = file('http://www.example.com/');
// 在数组中循环,显示 HTML 的源文件并加上行号。
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
// 另一个例子将 web 页面读入字符串。参见 file_get_contents()。
$html = implode('', file ('http://www.example.com/'));
?> 
note:返回的数组中每一行都包含了行结束符,如果不需要行结束符的话还需要使用rtrim()


is_file---判断给定文件名是否为一个正常文件

description

bool is_file(string $filename)

如果文件存在且正常则返回true,否则false

example

<?php
var_dump(is_file('a_file.txt')) . "\n";
var_dump(is_file('/usr/bin/')) . "\n";
?> 
output
bool(true)
bool(false)
note:

当返回false时会抛出一个e_warning警告,且此函数存在缓存


file_exists---检查文件或目录是否存在

description

bool file_exists(string $filename)

如果由$filename指定的文件或目录存在返回true,否则返回false

examples

<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
   echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?> 
note:

当返回false时会抛出一个e_warning 警告,且此函数存在缓存


filetype---取得文件类型

description

string filetype(string $filename)

返回的值可能有:fifo,char,dir,block,link,file 和 unknown

如果出错则返回false,如果stat调用失败或者文件类型未知的话filetype()还会产生一个e_notice 消息

examples

<?php
var_dump(filetype("test"));
var_dump(filetype("test/a/"));
var_dump(filetype("test.php"));
var_dump(filetype("7tian.gif"));
?>
output
string(3) "dir" string(3) "dir" string(4) "file" string(4) "file"
note:

当返回false时会抛出一个e_warning 警告,且此函数存在缓存


filesize---取得文件大小

description

int filesize(string $filename)
返回文件大小的字节数,如果出错返回false并生成一个e_warning级错误

example

<?php
var_dump(filesize("test.php"));
var_dump(filesize("7tian.gif"));
?>
output
int(36) int(418)
note:

当返回false时会抛出一个e_warning警告,且此函数存在缓存



fileatime---取得文件的上次访问时间

filectime---取得文件的inode修改时间

filegroup---取得文件的组

fileinode---取得文件的inode

filemtime---取得文件修改时间

fileowner---取得文件的所有者


is_readable---判断给定文件名是否可读

bool is_readable(string $filename)

如果filename指定的文件或者目录存在并且可读返回true,否则false

失败时抛出e_warning警告


is_writeable---判断给定文件名是否可写

bool is_writeable(string $filename)

如果文件存在且可写则返回true,filename可以是一个允许进行是否可写检查的目录名

失败时抛出e_warning警告. 


is_executable---判断给定文件名是否可执行

bool is_executable(string $filename)

如果文件存在且可执行则返回true,否则false

失败时抛出e_warning 警告. 


is_uploaded_file---判断文件是否是通过HTTP POST上传的

bool is_uploaded_file(string $filename)

如果 filename 所给出的文件是通过 HTTP POST 上传的则返回true。这可以用来确保恶意的用户无法欺骗脚本去访问本不能访问的文件,例如/etc/passwd。 


basename----返回路径中文件名部分

string basename(string $path[,string $suffix])

给出一个包含指向一个文件的全路径字符串,本函数返回基本文件名,如果文件名是以suffix结尾的,那这一部分也会被去掉

example

<?php
$path = "/home/httpd/html/index.php";
$a = basename($path);        
$b = basename($path,".php");
echo $a . '<br />' .$b;
?> 
output
index.php
index


pathinfo---返回文件目录信息

mixedpathinfo(string $path[,int $option])

pathinfo()返回一个包含path信息的关联数组,包含以下数组单元:dirname,basename,extension,filename

可以通过option指定返回那些元素,他们包括:PATHINFO_DIRNAME,PATHINFO_BASENAME,PATHINFO_EXTENSION

默认返回全部元素,如果指定了option,返回字符串

example

<?php
	$path_parts = pathinfo("/www/htdocs/index.html");
	print_R($path_parts);
	echo '<BR />';
	echo pathinfo("/www/htdocs/index.html",PATHINFO_DIRNAME).'<BR />';
	echo pathinfo("/www/htdocs/index.html",PATHINFO_BASENAME).'<BR />';
	echo pathinfo("/www/htdocs/index.html",PATHINFO_EXTENSION).'<BR />';
?> 
output
Array ( [dirname] => /www/htdocs [basename] => index.html [extension] => html [filename] => index ) 
/www/htdocs
index.html
html


rename---重命名一个文件或目录

bool rename(string $oldname,string $newname[,resource $context])

尝试将oldname重命名为newname,成功则返回true失败返回false

note:

可跨磁盘操作

example

<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?> 


copy---拷贝文件

bool copy(string $source, string $destination)

将文件从source拷贝到destination

example

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?> 
note

如果目标文件已存在,将会被覆盖

当copy零字节文件时,返回false,但文件也会正确复制



unlink---删除文件

bool unlink(string $filename)

删除指定文件,成功返回true失败false






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值