php文件操作类,本文件操作类可实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录.大家可参考参考.
实例1,代码如下:
- <?php
-
-
-
-
-
-
-
-
- class ClassFile
- {
- private $dir_name;
- private $file_name;
- private $dh;
-
-
-
-
- function __construct($PDirName)
- {
- $this->dir_name = $PDirName;
- if(is_dir($this->dir_name))
-
-
- if($this->dh = opendir($this->dir_name) or dir('路径错误,请重新检查路径!'));
- }
-
-
-
- function __destruct()
- {
-
- closedir($this->dh);
- }
-
-
-
- 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>";
- }
- }
- }
- }
-
-
-
- function DelFile($del_file)
- {
- if(is_file($del_file))
- {
-
- if(unlink($del_file))
- {
- echo "<script language='javascript'>";
- echo "alert('成功删除文件!');";
- echo "</script>";
- }
-
- }
- }
-
-
-
- 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";
- }
-
- }
- return $result_file;
- }
- }
- ?>
实例2,代码如下:
- <?php
-
-
-
-
-
-
-
-
- class fileoperate
- {
- var path;
- var name;
- var result;
-
-
-
-
-
-
-
- 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 "目录不存在,请检查";
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
- 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 "文件修改成功";
- }
- }
-
- }
-
-
-
-
-
-
-
- function del_file(path,name){
- filename=path.name;
-
- if (!file_exists(filename)) {
- echo "文件不存在,请确认路径是否正确";
- }
- else {
- if (unlink(filename)){
- echo "文件删除成功";
- }
- else echo "文件删除失败";
- }
-
- }
-
-
-
-
-
-
-
- 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>";
- }
-
-
-
-
-
-
-
-
- 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 "文件不存在";
- }
-
-
-
-
-
-
-
-
- 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 "文件不存在,无法移动";
- }
- }
- }
- ?>
实例3,代码如下:
这个比较高级了,不但可以实现文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录,还可以实现移动目录,copy,替换等操作,代码如下:
- <?php
- class FSC{
-
-
-
-
- function getfilesource($file){
- if($fp=fopen($file,'r')){
- $filesource=fread($fp,filesize($file));
- fclose($fp);
- return $filesource;
- }
- else
- return false;
- }
-
-
-
-
-
-
- function writefile($file,$source){
- if($fp=fopen($file,'w')){
- $filesource=fwrite($fp,$source);
- fclose($fp);
- return $filesource;
- }
- else
- return false;
- }
-
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
- 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);
- }
- }
-
-
-
-
-
-
- function notfate_mkdir($dir,$mode=0777){
- $u=umask(0);
- $r=mkdir($dir,$mode);
- umask($u);
- return $r;
- }
-
-
-
-
-
-
-
- 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;
- }
- }
-
- }
- ?>
|