Php文件系统函数

1.fopen();  //打开文件或者 URL (如果打开失败,函数返回 FALSE),成功返回resource

2.fclose();   //关闭一个已打开的文件指针  如果成功则返回 TRUE,失败则返回 FALSE

3.file_exists();  //检查文件或目录是否存在 返回true or false

4.filesize(); //取得文件大小  返回文件大小的字节数(bytes),如果出错返回 FALSE 并生成一条 E_WARNING 级的错误

5.filectime();//获取文件的创建时间   时间以 Unix 时间戳的方式返回,如果出错则返回 FALSE

6.filemtime(); //获取文件的修改时间   返回文件上次被修改的时间,出错时返回 FALSE。时间以 Unix时间戳的方式返回

7. fileatime();   //获取文件的上次访问时间 返回文件上次被访问的时间,如果出错则返回 FALSE。时间以Unix时间戳的方式返回

8.is_readable();  //如果由 filename 指定的文件或目录存在并且可读则返回 TRUE

9.is_writable();   //如果由 filename 指定的文件或目录存在并且可写则返回 TRUE

10.is_executable();    //如果由 filename 指定的文件或目录存在并且可执行则返回 TRUE


11.fwrite(); //写入文件

int fwrite ( resource handle, string string [, int length] ) //把 string 的内容写入 文件指针 handle 处。 如果指定了 length ,当写入了 length 个字节或者写完了 string 以后,写入就会停止,视乎先碰到哪种情况

<?php
$filename = 'a.txt';
$somecontent = "添加这些文字到文件\n";
$handle = fopen($filename, 'a');  //'a'写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。    
fwrite($handle, $somecontent);
fclose($handle);  //关闭一个已打开的文件指针
?>
12.fread(); //读取文件

string fread ( int handle, int length )从文件指针 handle,读取最多 length 个字节

<?php
$filename = "a.txt";
$handle = fopen($filename, "r"); //只读方式打开,将文件指针指向文件头。
$contents = fread($handle, filesize ($filename));
print_r($contents);  //输出内容
fclose($handle);
?>
13.feof();   //检测文件指针是否到了文件结束的位置 

如果文件指针到了 EOF 或者出错时则返回 TRUE,否则返回一个错误(包括 socket 超时),其它情况则返回 FALSE
14.fgets();  //从文件指针中读取一行

string fgets ( int handle [, int length] ) 从 handle 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。

<?php
$file = fopen("a.txt","r");
while(!feof($file))
  {
  echo fgets($file). "<br />";
  }
fclose($file);
?>

15.fgetc();   //从文件指针中读取字符

string fgetc ( resource $handle )

返回一个包含有一个字符的字符串,该字符从 handle 指向的文件中得到。碰到 EOF 则返回 FALSE

<?php
$handle = fopen('a.txt', 'r');
if (!$handle) {
    echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($handle))) {
    echo "$char\n";
}
fclose($handle);
?>
16.file();  //把整个文件读入一个数组中

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

数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败 file() 返回 FALSE

<?php
$lines = file('http://www.example.com/');
// 在数组中循环,显示 HTML 的源文件并加上行号。
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
?>

17.file_get_contents();   //将整个文件读入一个字符串


18.mkdir();    //新建目录

19.rmdir();    //删除目录  

尝试删除 dirname 所指定的目录。 该目录必须是空的,而且要有相应的权限。如果成功则返回 TRUE,失败则返回 FALSE

20.unlink();  //删除文件 如果成功则返回 TRUE,失败则返回 FALSE

21.copy();   //拷贝文件

bool copy ( string $source , string $dest )  将文件从 source 拷贝到 dest 。如果成功则返回 TRUE,失败则返回 FALSE

22.rename(); //重命名一个文件或目录  

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

23.dirname();  //返回路径中的目录部分

<?php
$path = "/etc/local/passwd";
$file = dirname($path);
print_r($file);  ///etc/local
?>
24.pathinfo(); //返回文件路径的信息
<?php
echo '<pre>';
print_r(pathinfo("/www/htdocs/index.html"));
echo '</pre>';
//Array
//(
//  [dirname] => /www/htdocs
//  [basename] => index.html
//  [extension] => html
//  [filename] => index
//)
?>
25.opendir();   //打开目录句柄 

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

如果成功则返回目录句柄的 resource,失败则返回 FALSE

26.readdir();  //从目录句柄中读取条目

string readdir ( resource $dir_handle )

返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回

<?php
if ($handle = opendir('F:/xampp/htdocs/www')) {
    echo "Directory handle: $handle\n";
    /* 这是正确地遍历目录方法 */
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }
    /* 这是错误地遍历目录的方法 */
//  while ($file = readdir($handle)) {
//      echo "$file\n";
//  }
    closedir($handle);
}
?>

27.is_uploaded_file();  //判断文件是否是通过 HTTP POST 上传的

bool is_uploaded_file ( string $filename )

28.move_uploaded_file();   //将上传的文件移动到新位置

bool move_uploaded_file ( string $filename , string $destination )

写一个函数,可以遍历文件夹下的所有文件和文件夹。

<?php
function get_dir_info($path){
          $handle = opendir($path);//打开目录返回句柄
          while(($content = readdir($handle))!== false){
                $new_dir = $path . DIRECTORY_SEPARATOR . $content;
                if($content == '..' || $content == '.'){
                       continue;
                }
                if(is_dir($new_dir)){
                      echo "<br>目录:".$new_dir . '<br>';
                      get_dir_info($new_dir);
                }else{
                      echo "文件:".$path.':'.$content .'<br>';
                }
          }
      }
get_dir_info($dir);
?>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值