| 以下為引用的內容: <?php /** *本類為文件操作類,實現了文件的建立,寫入,刪除,修改,復制,移動,創建目錄,刪除目錄 * 列出目錄里的文件等功能,路徑后面別忘了加"/" * * @author 路人郝 * @copyright myself * @link [url=http://www.phpr.cn]www.phpr.cn[/url] * */ class fileoperate { var path;// 文件路徑 var name;//文件名 var result;//對文件操作后的結果 /** * 本方法用來在path目錄下創建name文件 * * @param string path * @param string name */ function creat_file(path,name)//建立文件 { filename=path.name; if (file_exists(filename)) { echo "文件已經存在,請換個文件名"; } else { if (file_exists(path)) { touch(name); rename(name,filename); echo "文件建立成功 </br>"; } else{ echo "目錄不存在,請檢查"; } } } /** * 本方法用來寫文件,向path路徑下name文件寫入content內容,bool為寫入選項,值為1時 * 接著文件原內容下繼續寫入,值為2時寫入后的文件只有本次content內容 * * @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 "文件不存在,請確認路徑是否正確"; } else { if (unlink(filename)){ echo "文件刪除成功"; } else echo "文件刪除失敗"; } } /** * 本方法用來修改path目錄里name文件中的內容(可視) * * @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>文件內容"; echo "<p>"; echo "<input type='text' name='filename' value=filename />文件路徑<p>"; echo "<input name=ss type=submit value=修改文件內容 />"; echo "</form>"; } /** * 本方法用來復制name文件從spath到dpath * * @param string name * @param string spath * @param string dpath */ function copy_file(name,spath,dpath) //文件復制 { filename=spath.name; if (file_exists(filename)) { handle=fopen(filename,'a'); copy(filename,dpath.name); if (file_exists(dpath.name)) echo "文件復制成功"; else echo "文件復制失敗"; } else echo "文件不存在"; } /** * 本方法把name文件從spath移動到path路徑 * * @param string_type path * @param string_type dirname * @param string_type dpath */ function move_file(name,spath,dpath) //移動文件 { filename=spath.name; if (file_exists(filename)) { result=rename(filename,dpath.name); if (result==false or !file_exists(dpath)) echo "文件移動失敗或目的目錄不存在"; else echo "文件移動成功"; } else { echo "文件不存在,無法移動"; } } /** * 本方法把filename文件重命名為newname文件 * * @param string_type filename * @param string_type newname */ function rename_file(filename,newname) { //文件或目錄更名 path=pathinfo(filename); dir=path['dirname']; //得到文件路徑 newfilename=dir.newname; if (file_exists(filename)) { //判斷文件是否存在 result=rename(filename,newfilename); if (result==true) echo "文件更名成功"; else echo "文件更名失敗"; } else echo "文件不存在"; } /** * 本方法用來列出目錄里的文件或目錄switch為1時按字母順序列出所有目錄和文件 * switch為2則只列出目錄,switch為3時,只列出文件名 * * @param string_type path * @param int_type switch */ function list_filename(path,switch) //列出文件和目錄名 { if (file_exists(path)) { dir=scandir(path); if (switch==1){ //如果switch為1則按字母順序列出所有目錄和文件 for (i=0;i<=count(dir);i ) { if (dir[i]!="." and dir[i]!="..") { echo "dir[i]<br>"; } } } if (switch==2) //switch為2則只列出目錄 { for (i=0;i<=count(dir);i ) { x=is_dir(path.dir[i]); if (dir[i]!="." and dir[i]!=".." and x==true) { echo "dir[i]<br>"; } } } if (switch==3) //switch為3時,只列出文件名 { for (i=0;i<=count(dir);i ) { x=is_dir(path.dir[i]); if (dir[i]!="." and dir[i]!=".." and x==false) { echo "dir[i]<br>"; } } } } } /** * 本方法在path目錄下創建名為dirname的目錄 * * @param string_type path * @param string_type dirname */ function creat_dir(path,dirname){ //創建目錄 if (file_exists(path)) { result=mkdir(path.dirname); if (result) echo "目錄建立成功"; else echo "目錄建立失敗"; } else echo "路徑不存在,請重新輸入"; } /** * 本方法刪除pathname目錄,包括該目錄下所有的文件及子目錄 * * @param string_type pathname */ function del_dir(pathname){ //刪除目錄及目錄里所有的文件夾和文件 if (!is_dir(pathname)) {exit("你輸入的不是一個目錄,請檢查") ;} handle=opendir(pathname); while ((fileordir=readdir(handle)) !== false) { if (fileordir!="." && fileordir!="..") { is_dir("pathname/fileordir")? this->del_dir("pathname/fileordir"): unlink("pathname/fileordir"); } } if (readdir(handle) == false) { closedir(handle); rmdir(pathname); } } } ?> modi_file.php <?php include('file.class.php'); content=_POST['content'];//得到文件內容 //echo content;// filename=_POST['filename']; path=pathinfo(filename); //得到文件路徑 dir=path['dirname']."/"; //目錄名 name=path['basename']; //文件名 op=new fileoperate(); op->write_file("dir","name",content,2); ?> test.php <?php include('file.class.php'); op=new fileoperate(); //op->creat_file("./","aa3.txt"); //op->del_file("./","aa3.txt"); //b="fdfdsfdsfdsfdfdfdfdfdsfdsf1233456"; //op->write_file("./","aa3.txt",b,1); //op->modi_file("./","aa3.txt"); //op->copy_file("aa3.txt","./","../a/"); //op->move_file("aa3.txt","../","./"); //op->rename_file("aa3.txt","aa2.txt"); //op->list_filename("../",3); //op->creat_dir("/wwwroot","wesley"); //op->del_dir("/aaa/test");?> |