Discuz教程网

文件与目录操作常用的函数

[复制链接]
authicon Confi-Jin 发表于 2012-3-10 19:11:28 | 显示全部楼层 |阅读模式
<?php
//文件与目录操作
//一、文件的基本操作
//1:打开文件  fopen    fopen ( string $filename , string $mode )返回值是资源类型
  /* string $mode 参数二的几种形式
   'r' 只读方式打开,将文件指针指向文件头。  
   'r+' 读写方式打开,将文件指针指向文件头。  
   'w' 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。  
   'w+' 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。  
   'a' 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。  
   'a+' 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。  
   'x' 创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在2012/3/2则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。  
   'x+' 创建并以读写方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。

  */
//$handle = fopen("abc.txt", "r");
//var_dump($handle);
//echo $handle;
//$handle = fopen("/home/rasmus/file.gif", "wb");
//$handle = fopen("
http://www.example.com/", "r");
//$handle = fopen("
ftp://user:password@example.com/somefile.txt", "w");

//2:关闭文件  fclose   fclose — 关闭一个已打开的文件指针  返回的是布尔型
  //$a = fclose($handle);
  //var_dump($a);

//2:  创建文件    fopen/touch
  //touch("a3.txt");
//3:  删除文件    unlink
  //$filename = "aaa.txt";
  //$aa = unlink($filename);
  //var_dump($aa);
//4:  锁定文件  flock ( int $handle , int $operation [, int &$wouldblock ] )
    //操作的 handle 必须是一个已经打开的文件指针。operation 可以是以下值之一:
  /*
   要取得共享锁定(读取的程序),将 operation 设为 LOCK_SH
   要取得独占锁定(写入的程序),将 operation 设为 LOCK_EX
   要释放锁定(无论共享或独占),将 operation 设为 LOCK_UN
   如果不希望 flock() 在锁定时堵塞,则给 operation 加上 LOCK_NB
  
  $fp = fopen("./aaa.txt", "w+");

  if (flock($fp, LOCK_EX)) { // 进行排它型锁定
   fwrite($fp, "Write something here\n");
   flock($fp, LOCK_UN); // 释放锁定
  } else {
   echo "Couldn't lock the file !";
  }

   fclose($fp);
*/
//5:  写入文件    fwrite/fputs/file_put_contents   
      //fwrite — 写入文件(可安全用于二进制文件)
  /*$filename = "a.txt";     //文件名
  $filetxt  = fopen($filename,'a');  //打开文件

  $filezi   = "hello\n";     //写入文字
  $file     = fwrite($filetxt,$filezi);   //写入文件
  fclose($file);
  */
//6:  读取文件    fread/fgets/file_get_contents/file/readfile

   /*   fread     string fread ( int $handle , int $length )
   
   $filename = "./a.txt";
   $filetxt  = fopen($filename,'r');
   $str = fread($filetxt, filesize($filename));
   var_dump($str);
   fclose($filetxt);
   */
   // fgets       string fgets ( int $handle [, int $length ] )
   /*
   $handle = @fopen("./abc.txt", "r");
   if ($handle) {
    while (!feof($handle)) {
     $buffer = fgets($handle, 4096);
     echo $buffer;
    }
   fclose($handle);
   }  
   */
   //file_get_contents()   将文件作为一个数组返回
    /*
    $data = './a.txt';
    // 1.98 seconds

    $fh = fopen('./abc.txt', 'r');
    $data1 = fread($fh, filesize('./abc.txt'));
    echo $data1;
   
    echo date("Y-m-d");
    echo "<br/>";
    fclose($fh);
    // 0.00082 seconds

    $data1 = file_get_contents('./abc.txt');
    echo $data1;
    // 0.0069 seconds
    */
   //file()将文件作为一个数组返回
    /*$lines = file("./abc.txt");

     foreach($lines as $line){
     //echo($line);
     var_dump ($lines);
    }
    */
   //readfile — 输出一个文件读入一个文件并写入到输出缓冲
//7:  拷贝、转移文件   copy/rename
  //copy  ( string $source , string $dest )  拷贝文件   可以使文件重命名并且源文件还存在  
     //不可以复制目录
   /*同级文件copy
   $file = 'a.txt';
   $newfile = 'aa.txt';
   */
   /* 不同级文件copy
   $file = '../aa.txt';
   $newfile = 'sssa.txt';
   
   if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
   }
   */

   //rename ( string $oldname , string $newname [, resource $context ] )
    //rename("aaasss.txt", "../aaa.txt");//重名文件并转移文件
    //rename("../aa","bb");//移动并重命名
//8:  文件常用函数     feof/file_exists/is_file/filesize/is_link/filetype/

  //feof — 测试文件指针是否到了文件结束的位置
  //file_exists  、
   /*检测文件是否存在
   $filename = 'abc.txt';

   if (file_exists($filename)) {
    echo "The file $filename exists";
   } else {
    echo "The file $filename does not exist";
   }
   */
   /*检测目录是否存在
   $filename = 'bb';

   if (file_exists($filename)) {
    echo "The file $filename exists";
   } else {
    echo "The file $filename does not exist";
   }
   */
  //is_file — 判断给定文件名是否为一个正常的文件
   //var_dump(is_file('aaa.txt')) . "\n";
  //filesize — 取得文件大小
   /*
   $filename = 'abc.txt';
   echo $filename . ': ' . filesize($filename) . ' bytes';
   */
   /*取得文件目录的大小
   $filename = '../xml/';
   
   echo $filename . ': ' . filesize($filename) . ' bytes';
   */
  //is_link
  //filetype取得文件和目录的类型
   //echo filetype('abc.txt');  // file
   //echo filetype('bb');        // dir
//9: 权限相关函数  is_readable/is_writeable/is_executable/chown/chgrp/chmod/filegroup
  //is_readable 判断给定文件名是否可读
   /*
   $filename = 'abc.txt';
   if (is_readable($filename)) {
    echo 'The file is readable';
   } else {
    echo 'The file is not readable';
   }
   */
//10: 文件时间函数  filectime/fileatime/filemtime
/*
//filectime()创建时间
  $t=filectime('./abc.txt');
  echo date("Y-m-d H:i:s",$t);
  echo "<br/>";
//fileatime()修改时间
  $tt=fileatime('./abc.txt');
  echo date("Y-m-d H:i:s",$tt);
  echo "<br/>";
//filemmm time()修改时间
  $ttt=filemtime('./abc.txt');
  echo date("Y-m-d H:i:s",$ttt);
  echo "<br/>";
//filectime()目录的创建时间
  $t=filectime('./bbb');
  echo date("Y-m-d H:i:s",$t);
  echo "<br/>";

*/

//二、目录操作
//1: 基本操作   opendir/readdir/closedir
//opendir — 打开目录句柄   
//readdir — 从目录句柄中读取条目  
//closedir — 关闭目录句柄
  /*if ($handle = opendir('D:\web\php')) {
   echo "Directory handle: $handle\n";
   echo "Files:\n";

   // 这是正确地遍历目录方法
   while (false !== ($file = readdir($handle))) {
    echo "$file\n";
   }
   closedir($handle);
  }
  */
  //scandir — 列出指定路径中的文件和目录

//2:  相关操作函数  is_dir/mkdir/rmdir/chdir/getcwd/rewinddir
//is_dir — 判断给定文件名是否是一个目录   返回的是布尔型
  //clearstatcache — 清除文件状态缓存
  //var_dump(is_dir('abc.txt')) . "\n";
  //var_dump(is_dir('./bb')) . "\n";

  //var_dump(is_dir('..')); //one dir up
  //clearstatcache();
//mkdir — 新建目录
  //mkdir("bbb", 0700);//参数二是访问权限
//rmdir — 删除目录   该目录必须是空的,而且要有相应的权限
  //rmdir("bb");
//chdir — 改变目录   //不能使用当前所在目录   可以看到上一级目录
  // current directory
  /*
  echo getcwd() . "\n";

  chdir('../cache');
  // current directory
  echo getcwd() . "\n";
  */
//getcwd — 取得当前工作目录  取得当前目录的全部路径
  //echo getcwd();
//rewinddir — 倒回目录句柄


if(is_dir('C:\abc')){
  //echo 1111;
  opendir('C:\abc');
} else {
  //echo 222;
  mkdir('C:\abc');
  echo 111;

}



上一篇:&quot;test.jd150.com&quot;申请免费空间
下一篇:zjqt.cn申请个人免费空间
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 14:22

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表