Home > php教程 > php手册 > body text

PHP面向对象文件操作类

WBOY
Release: 2016-06-21 09:07:28
Original
1394 people have browsed it

对象

 
 /*
  文件名:ClassFile.php
  功能:面向对象,文件操作类(查看文件,删除文件,上传文件)
  作者:感染源
  时间:2007-1-11

 */
 
 class ClassFile
 {
  private $dir_name;
  private $file_name;
  private $dh;
  //private $file_path;
  
  
  //构造函数,打开工作文件目录
  function __construct($PDirName)
  {
   $this->dir_name = $PDirName;
   if(is_dir($this->dir_name))
    
    //打开目录
    if($this->dh = opendir($this->dir_name) or dir('路径错误,请重新检查路径!'));
  }//function __construct();
  
  
  //关闭文件目录
  function __destruct()
  {
   //关闭目录
   closedir($this->dh);
  }//function __destruct();
  
  
  //向当前文件夹添加文件,同时检查是否有重名的文件
  function AddFile($temp_file)
  {
   //读取目录
   while($this->file_name = readdir($this->dh))
   {
    if($this->file_name==$temp_file)  //检查是否和已存在的文件重名
    {
     echo '文件重名,请更改文件名...';
     exit;
    }
    else
    {
     //将临时文件复制到当前目录中
     $up_id = copy($temp_file,"$this->dir_name/$temp_file");
     if(!$up_id)
     {
      echo '文件上传失败,请重新再传...';
      exit;
     }
     else
     {
      unlink($temp_file);  //删除临时文件
      
      echo "";
     }//if
    }//if
   }//while
  }//function AddFile();
  
  
  //删除文件
  function DelFile($del_file)
  {
   if(is_file($del_file))
   {
    //$path = "$this->dir_name\/$del_file";
    if(unlink($del_file))
    {
     echo "";
    }
     
   }//if
  }//function DelFile();
  
  
  //读取文件
  function Read_File()
  {
    $result_file = array();
    $i = 0;
   while($this->file_name = readdir($this->dh))
   {
    if($this->file_name=='.'||$this->file_name=='..')
    {}
    else
    {
     $result_file[$i++] = "$this->dir_name/$this->file_name";
    }//if
    
   }//while
   return $result_file; //返回文件名数组
  }//function Read_File()
 }//Class ClassFile
?> 



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template