php文件操作类

 
更多
<?php
class IO
{
    private $path; // 文件路
    private $name; //文件名
    private $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);
        }
    }
}
?>
打赏

本文固定链接: https://www.cxy163.net/archives/311 | 绝缘体

该日志由 绝缘体.. 于 2012年03月17日 发表在 PHP 分类下,
原创文章转载请注明: php文件操作类 | 绝缘体

报歉!评论已关闭.