PHP物件導向檔案操作類

來源:互聯網
上載者:User
對象 <?php
 
 /*
  檔案名稱: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 "<script language='javascript'>";
      echo "alert(\"檔案上傳成功!\");";
      echo "</script>";
     }//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 "<script language='javascript'>";
     echo "alert(\'成功刪除檔案!\');";
     echo "</script>";
    }
     
   }//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
?> 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.