PHP-非常好用的文件操作类
时间:2010-09-28 来源:peishuai1987
<?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文件
*
* @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 "文件删除失败";
}
}
/**
* 本方法把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);
}
}
}
?>
<?php
$op=new fileoperate();
$op->creat_file("user-files/","aa3.txt");
//$op->del_file("user-files/","aa3.txt");
//$op->rename_file("user-files/aa3.txt","测试文件.txt");
echo '<br>';
$op->list_filename("user-files/",1);
$op->creat_dir("user-files/","新建的目录");
$op->del_dir("user-files/新建的目录");
?>