php文件操作類,本文件操作類可實(shí)現(xiàn)了文件的建立,寫入,刪除,修改,復(fù)制,移動(dòng),創(chuàng)建目錄,刪除目錄.大家可參考參考.
實(shí)例1,代碼如下:
- <?php
- /*
- 文件名:ClassFile.php
- 功能:面向?qū)ο螅募僮黝?查看文件,刪除文件,上傳文件)
- 作者:感染源
- 時(shí)間:2007-1-11
- */
- class ClassFile
- {
- private $dir_name;
- private $file_name;
- private $dh;
- //private $file_path;
- //構(gòu)造函數(shù),打開工作文件目錄
- function __construct($PDirName)
- {
- $this->dir_name = $PDirName;
- if(is_dir($this->dir_name))
- //打開目錄
- if($this->dh = opendir($this->dir_name) or dir('路徑錯(cuò)誤,請(qǐng)重新檢查路徑!'));
- }//function __construct();
- //關(guān)閉文件目錄
- function __destruct()
- {
- //關(guān)閉目錄
- closedir($this->dh);
- }//function __destruct();
- //向當(dāng)前文件夾添加文件,同時(shí)檢查是否有重名的文件
- function AddFile($temp_file)
- {
- //讀取目錄
- while($this->file_name = readdir($this->dh))
- {
- if($this->file_name==$temp_file) //檢查是否和已存在的文件重名
- {
- echo '文件重名,請(qǐng)更改文件名...';
- exit;
- }
- else
- {
- //將臨時(shí)文件復(fù)制到當(dāng)前目錄中
- $up_id = copy($temp_file,"$this->dir_name/$temp_file");
- if(!$up_id)
- {
- echo '文件上傳失敗,請(qǐng)重新再傳...';
- exit;
- }
- else
- {
- unlink($temp_file); //刪除臨時(shí)文件
- 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; //返回文件名數(shù)組
- }//function Read_File()
- }//Class ClassFile
- ?>
實(shí)例2,代碼如下:
- <?php
- /**
- *本類為文件操作類,實(shí)現(xiàn)了文件的建立,寫入,刪除,修改,復(fù)制,移動(dòng),創(chuàng)建目錄,刪除目錄
- * 列出目錄里的文件等功能,路徑后面別忘了加"/"
- *
- * @author 路人郝
- * @copyright myself
- *
- */
- class fileoperate
- {
- var path;// 文件路徑
- var name;//文件名
- var result;//對(duì)文件操作后的結(jié)果
- /**
- * 本方法用來在path目錄下創(chuàng)建name文件
- *
- * @param string path
- * @param string name
- */
- function creat_file(path,name)//建立文件
- {
- filename=path.name;
- if (file_exists(filename))
- {
- echo "文件已經(jīng)存在,請(qǐng)換個(gè)文件名";
- }
- else
- {
- if (file_exists(path))
- {
- touch(name);
- rename(name,filename);
- echo "文件建立成功 </br>";
- }
- else{
- echo "目錄不存在,請(qǐng)檢查";
- }
- }
- }
- /**
- * 本方法用來寫文件,向path路徑下name文件寫入content內(nèi)容,bool為寫入選項(xiàng),值為1時(shí)
- * 接著文件原內(nèi)容下繼續(xù)寫入,值為2時(shí)寫入后的文件只有本次content內(nèi)容
- *
- * @param string_type path
- * @param string_type name
- * @param string_type content
- * @param bool_type bool
- */
- function write_file(path,name,content,bool) //寫文件
- {
- filename=path.name;
- if (bool==1) {
- if (is_writable(filename)) {
- handle=fopen(filename,'a');
- if (!handle) {
- echo "文件不能打開或文件不存在";
- exit;
- }
- result=fwrite(handle,content);
- if (!result) {
- echo "文件寫入失敗";
- }
- echo "文件寫入成功";
- fclose(handle);
- }
- else echo "文件不存在";
- }
- if (bool==2) {
- if (!file_exists(filename)) {
- this->creat_file(path,name);
- handle=fopen(filename,'a');
- if (fwrite(handle,content));
- echo "文件寫入成功";
- }
- else {
- unlink(filename);
- this->creat_file(path,name);
- this->write_file(path,name,content,1);
- echo "文件修改成功";
- }
- }
- }
- /**
- * 本方法刪除path路徑下name文件
- *
- * @param string_type path
- * @param string_type name
- */
- function del_file(path,name){ //刪除文件
- filename=path.name;
- if (!file_exists(filename)) {
- echo "文件不存在,請(qǐng)確認(rèn)路徑是否正確";
- }
- else {
- if (unlink(filename)){
- echo "文件刪除成功";
- }
- else echo "文件刪除失敗";
- }
- }
- /**
- * 本方法用來修改path目錄里name文件中的內(nèi)容(可視)
- *
- * @param string_type path
- * @param string_type name
- */
- function modi_file(path,name){ //文件修改
- filename=path.name;
- handle=fopen(filename,'r+');
- content=file_get_contents(filename);
- echo "<form id='form1' name='form1' action='modi_file.php' method='post'>";
- echo "<textarea name=content rows='10'>content</textarea>文件內(nèi)容";
- echo "<p>";
- echo "<input type='text' name='filename' value=filename />文件路徑<p>";
- echo "<input name=ss type=submit value=修改文件內(nèi)容 />";
- echo "</form>";
- }
- /**
- * 本方法用來復(fù)制name文件從spath到dpath
- *
- * @param string name
- * @param string spath
- * @param string dpath
- */
- function copy_file(name,spath,dpath) //文件復(fù)制
- {
- filename=spath.name;
- if (file_exists(filename)) {
- handle=fopen(filename,'a');
- copy(filename,dpath.name);
- if (file_exists(dpath.name))
- echo "文件復(fù)制成功";
- else echo "文件復(fù)制失敗";
- }
- else echo "文件不存在";
- }
- /**
- * 本方法把name文件從spath移動(dòng)到path路徑
- *
- * @param string_type path
- * @param string_type dirname
- * @param string_type dpath
- */
- function move_file(name,spath,dpath) //移動(dòng)文件
- {
- filename=spath.name;
- if (file_exists(filename)) {
- result=rename(filename,dpath.name);
- if (result==false or !file_exists(dpath))
- echo "文件移動(dòng)失敗或目的目錄不存在";
- else
- echo "文件移動(dòng)成功";
- }
- else {
- echo "文件不存在,無法移動(dòng)";
- }
- }
- }
- ?>
實(shí)例3,代碼如下:
這個(gè)比較高級(jí)了,不但可以實(shí)現(xiàn)文件的建立,寫入,刪除,修改,復(fù)制,移動(dòng),創(chuàng)建目錄,刪除目錄,還可以實(shí)現(xiàn)移動(dòng)目錄,copy,替換等操作,代碼如下:
- <?php
- class FSC{
- // 函數(shù)名: getfilesource
- // 功能: 得到指定文件的內(nèi)容
- // 參數(shù): $file 目標(biāo)文件
- // test passed
- function getfilesource($file){
- if($fp=fopen($file,'r')){
- $filesource=fread($fp,filesize($file));
- fclose($fp);
- return $filesource;
- }
- else
- return false;
- }
- // 函數(shù)名: writefile
- // 功能: 創(chuàng)建新文件,并寫入內(nèi)容,如果指定文件名已存在,那將直接覆蓋
- // 參數(shù): $file -- 新文件名
- // $source 文件內(nèi)容
- //test passed
- function writefile($file,$source){
- if($fp=fopen($file,'w')){
- $filesource=fwrite($fp,$source);
- fclose($fp);
- return $filesource;
- }
- else
- return false;
- }
- // 函數(shù)名: movefile
- // 功能: 移動(dòng)文件
- // 參數(shù): $file -- 待移動(dòng)的文件名
- // $destfile -- 目標(biāo)文件名
- // $overwrite 如果目標(biāo)文件存在,是否覆蓋.默認(rèn)是覆蓋.
- // $bak 是否保留原文件 默認(rèn)是不保留即刪除原文件
- // test passed
- function movefile($file,$destfile,$overwrite=1,$bak=0){
- if(file_exists($destfile)){
- if($overwrite)
- unlink($destfile);
- else
- return false;
- }
- if($cf=copy($file,$destfile)){
- if(!$bak)
- return(unlink($file));
- }
- return($cf);
- }
- // 函數(shù)名: movedir
- // 功能: 這是下一涵數(shù)move的附助函數(shù),功能就是移動(dòng)目錄
- function movedir($dir,$destdir,$overwrite=1,$bak=0){
- @set_time_limit(600);
- if(!file_exists($destdir))
- FSC::notfate_any_mkdir($destdir);
- if(file_exists($dir)&&(is_dir($dir)))
- {
- if(substr($dir,-1)!='/')$dir.='/';
- if(file_exists($destdir)&&(is_dir($destdir))){
- if(substr($destdir,-1)!='/')$destdir.='/';
- $h=opendir($dir);
- while($file=readdir($h)){
- if($file=='.'||$file=='..')
- {
- continue;
- $file="";
- }
- if(is_dir($dir.$file)){
- if(!file_exists($destdir.$file))
- FSC::notfate_mkdir($destdir.$file);
- else
- chmod($destdir.$file,0777);
- FSC::movedir($dir.$file,$destdir.$file,$overwrite,$bak);
- FSC::delforder($dir.$file);
- }
- else
- {
- if(file_exists($destdir.$file)){
- if($overwrite)unlink($destdir.$file);
- else{
- continue;
- $file="";
- }
- }
- if(copy($dir.$file,$destdir.$file))
- if(!$bak)
- if(file_exists($dir.$file)&&is_file($dir.$file))
- @unlink($dir.$file);
- }
- }
- }
- else
- return false;
- }
- else
- return false;
- }
- // 函數(shù)名: move
- // 功能: 移動(dòng)文件或目錄
- // 參數(shù): $file -- 源文件/目錄
- // $path -- 目標(biāo)路徑
- // $overwrite -- 如是目標(biāo)路徑中已存在該文件時(shí),是否覆蓋移動(dòng)
- // -- 默認(rèn)值是1, 即覆蓋
- // $bak -- 是否保留備份(原文件/目錄)
- function move($file,$path,$overwrite=1,$bak=0)
- {
- if(file_exists($file)){
- if(is_dir($file)){
- if(substr($file,-1)=='/')$dirname=basename(substr($file,0,strlen($file)-1));
- else $dirname=basename($file);
- if(substr($path,-1)!='/')$path.='/';
- if($file!='.'||$file!='..'||$file!='../'||$file!='./')$path.=$dirname;
- FSC::movedir($file,$path,$overwrite,$bak);
- if(!$bak)FSC::delforder($file);
- }
- else{
- if(file_exists($path)){
- if(is_dir($path))chmod($path,0777);
- else {
- if($overwrite)
- @unlink($path);
- else
- return false;
- }
- }
- else
- FSC::notfate_any_mkdir($path);
- if(substr($path,-1)!='/')$path.='/';
- FSC::movefile($file,$path.basename($file),$overwrite,$bak);
- }
- }
- else
- return false;
- }
- // 函數(shù)名: delforder
- // 功能: 刪除目錄,不管該目錄下是否有文件或子目錄,全部刪除哦,小心別刪錯(cuò)了哦!
- // 參數(shù): $file -- 源文件/目錄
- //test passed
- function delforder($file) {
- chmod($file,0777);
- if (is_dir($file)) {
- $handle = opendir($file);
- while($filename = readdir($handle)) {
- if ($filename != "." && $filename != "..")
- {
- FSC::delforder($file."/".$filename);
- }
- }
- closedir($handle);
- return(rmdir($file));
- }
- else {
- unlink($file);
- }
- }
- // 函數(shù)名: notfate_mkdir
- // 功能: 創(chuàng)建新目錄,這是來自php.net的一段代碼.彌補(bǔ)mkdir的不足.
- // 參數(shù): $dir -- 目錄名
- function notfate_mkdir($dir,$mode=0777){
- $u=umask(0);
- $r=mkdir($dir,$mode);
- umask($u);
- return $r;
- }
- // 函數(shù)名: notfate_any_mkdir
- // 功能: 創(chuàng)建新目錄,與上面的notfate_mkdir有點(diǎn)不同,因?yàn)樗嗔艘粋€(gè)any,即可以創(chuàng)建多級(jí)目錄
- // 如:notfate_any_mkdir("abc/abc/abc/abc/abc")
- // 參數(shù): $dirs -- 目錄名
- function notfate_any_mkdir($dirs,$mode=0777)
- {
- if(!strrpos($dirs,'/'))
- {
- return(FSC::notfate_mkdir($dirs,$mode));
- }else
- {
- $forder=explode('/',$dirs);
- $f='';
- for($n=0;$n<count($forder);$n++)
- {
- if($forder[$n]=='') continue;
- $f.=((($n==0)&&($forder[$n]<>''))?(''):('/')).$forder[$n];
- if(file_exists($f)){
- chmod($f,0777);
- continue;
- }
- else
- {
- if(FSC::notfate_mkdir($f,$mode)) continue;
- else
- return false;
- }
- }
- return true;
- }
- }
- }
- ?>
新聞熱點(diǎn)
疑難解答