PHP文件操作类

Jun 23, 2016 pm 02:28 PM

简介:这是PHP文件操作类的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=335093' scrolling='no'>
/*** 操纵文件类* * 例子:* FileUtil::createDir('a/1/2/3');                    测试建立文件夹    建一个a/1/2/3文件夹* FileUtil::createFile('b/1/2/3');                    测试建立文件        在b/1/2/文件夹下面建一个3文件* FileUtil::createFile('b/1/2/3.exe');                测试建立文件        在b/1/2/文件夹下面建一个3.exe文件* FileUtil::copyDir('b','d/e');                    测试复制文件夹    建立一个d/e文件夹,把b文件夹下的内容复制进去* FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe');    测试复制文件        建立一个b/b文件夹,并把b/1/2文件夹中的3.exe文件复制进去* FileUtil::moveDir('a/','b/c');                    测试移动文件夹    建立一个b/c文件夹,并把a文件夹下的内容移动进去,并删除a文件夹* FileUtil::moveFile('b/1/2/3.exe','b/d/3.exe');    测试移动文件        建立一个b/d文件夹,并把b/1/2中的3.exe移动进去                    * FileUtil::unlinkFile('b/d/3.exe');                测试删除文件        删除b/d/3.exe文件* FileUtil::unlinkDir('d');                        测试删除文件夹    删除d文件夹*/class FileUtil {    /**     * 建立文件夹     *     * @param    string $aimUrl     * @return    viod     */    function createDir($aimUrl) {        $aimUrl = str_replace('', '/', $aimUrl);        $aimDir = '';        $arr = explode('/', $aimUrl);        foreach ($arr as $str) {            $aimDir .= $str . '/';            if (!file_exists($aimDir)) {                mkdir($aimDir);            }        }    }    /**     * 建立文件     *     * @param    string    $aimUrl      * @param    boolean    $overWrite 该参数控制是否覆盖原文件     * @return    boolean     */    function createFile($aimUrl, $overWrite = false) {        if (file_exists($aimUrl) && $overWrite == false) {            return false;        } elseif (file_exists($aimUrl) && $overWrite == true) {            FileUtil::unlinkFile($aimUrl);        }        $aimDir = dirname($aimUrl);        FileUtil::createDir($aimDir);        touch($aimUrl);        return true;    }    /**     * 移动文件夹     *     * @param    string    $oldDir     * @param    string    $aimDir     * @param    boolean    $overWrite 该参数控制是否覆盖原文件     * @return    boolean     */    function moveDir($oldDir, $aimDir, $overWrite = false) {        $aimDir = str_replace('', '/', $aimDir);        $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';        $oldDir = str_replace('', '/', $oldDir);        $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';        if (!is_dir($oldDir)) {            return false;        }        if (!file_exists($aimDir)) {            FileUtil::createDir($aimDir);        }        @$dirHandle = opendir($oldDir);        if (!$dirHandle) {            return false;        }        while(false !== ($file = readdir($dirHandle))) {            if ($file == '.' || $file == '..') {                continue;            }            if (!is_dir($oldDir.$file)) {                FileUtil::moveFile($oldDir . $file, $aimDir . $file, $overWrite);            } else {                FileUtil::moveDir($oldDir . $file, $aimDir . $file, $overWrite);            }        }        closedir($dirHandle);        return rmdir($oldDir);    }    /**     * 移动文件     *     * @param    string    $fileUrl     * @param    string    $aimUrl     * @param    boolean    $overWrite 该参数控制是否覆盖原文件     * @return    boolean     */    function moveFile($fileUrl, $aimUrl, $overWrite = false) {        if (!file_exists($fileUrl)) {            return false;        }        if (file_exists($aimUrl) && $overWrite = false) {            return false;        } elseif (file_exists($aimUrl) && $overWrite = true) {            FileUtil::unlinkFile($aimUrl);        }        $aimDir = dirname($aimUrl);        FileUtil::createDir($aimDir);        rename($fileUrl, $aimUrl);        return true;    }    /**     * 删除文件夹     *     * @param    string    $aimDir     * @return    boolean     */    function unlinkDir($aimDir) {        $aimDir = str_replace('', '/', $aimDir);        $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/';        if (!is_dir($aimDir)) {            return false;        }        $dirHandle = opendir($aimDir);        while(false !== ($file = readdir($dirHandle))) {            if ($file == '.' || $file == '..') {                continue;            }            if (!is_dir($aimDir.$file)) {                FileUtil::unlinkFile($aimDir . $file);            } else {                FileUtil::unlinkDir($aimDir . $file);            }        }        closedir($dirHandle);        return rmdir($aimDir);    }    /**     * 删除文件     *     * @param    string    $aimUrl     * @return    boolean     */    function unlinkFile($aimUrl) {        if (file_exists($aimUrl)) {            unlink($aimUrl);            return true;        } else {            return false;        }    }    /**     * 复制文件夹     *     * @param    string    $oldDir     * @param    string    $aimDir     * @param    boolean    $overWrite 该参数控制是否覆盖原文件     * @return    boolean     */    function copyDir($oldDir, $aimDir, $overWrite = false) {        $aimDir = str_replace('', '/', $aimDir);        $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/';        $oldDir = str_replace('', '/', $oldDir);        $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir.'/';        if (!is_dir($oldDir)) {            return false;        }        if (!file_exists($aimDir)) {            FileUtil::createDir($aimDir);        }        $dirHandle = opendir($oldDir);        while(false !== ($file = readdir($dirHandle))) {            if ($file == '.' || $file == '..') {                continue;            }            if (!is_dir($oldDir . $file)) {                FileUtil::copyFile($oldDir . $file, $aimDir . $file, $overWrite);            } else {                FileUtil::copyDir($oldDir . $file, $aimDir . $file, $overWrite);            }        }        return closedir($dirHandle);    }    /**     * 复制文件     *     * @param    string    $fileUrl     * @param    string    $aimUrl     * @param    boolean    $overWrite 该参数控制是否覆盖原文件     * @return    boolean     */    function copyFile($fileUrl, $aimUrl, $overWrite = false) {        if (!file_exists($fileUrl)) {            return false;        }        if (file_exists($aimUrl) && $overWrite == false) {            return false;        } elseif (file_exists($aimUrl) && $overWrite == true) {            FileUtil::unlinkFile($aimUrl);        }        $aimDir = dirname($aimUrl);        FileUtil::createDir($aimDir);        copy($fileUrl, $aimUrl);        return true;    }}?><?phpclass FileUtil {//建立文件夹    function createDir($aimUrl) {        $aimUrl = str_replace('', '/', $aimUrl);        $aimDir = '';        $arr = explode('/', $aimUrl);        foreach ($arr as $str) {            $aimDir .= $str . '/';            if (!file_exists($aimDir)) {                mkdir($aimDir);            }        }    }// 建立文件    function createFile($aimUrl, $overWrite = false) {        if (file_exists($aimUrl) && $overWrite == false) {            return false;        } elseif (file_exists($aimUrl) && $overWrite == true) {            FileUtil::unlinkFile($aimUrl);        }        $aimDir = dirname($aimUrl);        FileUtil::createDir($aimDir);        touch($aimUrl);        return true;    }//移动文件夹    function moveDir($oldDir, $aimDir, $overWrite = false) {        $aimDir = str_replace('', '/', $aimDir);        $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';        $oldDir = str_replace('', '/', $oldDir);        $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';        if (!is_dir($oldDir)) {            return false;        }        if (!file_exists($aimDir)) {            FileUtil::createDir($aimDir);        }        @$dirHandle = opendir($oldDir);        if (!$dirHandle) {            return false;        }        while(false !== ($file = readdir($dirHandle))) {            if ($file == '.' || $file == '..') {                continue;            }            if (!is_dir($oldDir.$file)) {                FileUtil::moveFile($oldDir . $file, $aimDir . $file, $overWrite);            } else {                FileUtil::moveDir($oldDir . $file, $aimDir . $file, $overWrite);            }        }        closedir($dirHandle);        return FileUtil::unlinkDir($oldDir);    }//移动文件    function moveFile($fileUrl, $aimUrl, $overWrite = false) {        if (!file_exists($fileUrl)) {            return false;        }        if (file_exists($aimUrl) && $overWrite = false) {            return false;        } elseif (file_exists($aimUrl) && $overWrite = true) {            FileUtil::unlinkFile($aimUrl);        }        $aimDir = dirname($aimUrl);        FileUtil::createDir($aimDir);        rename($fileUrl, $aimUrl);        return true;    }//删除文件夹    function unlinkDir($aimDir) {        $aimDir = str_replace('', '/', $aimDir);        $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/';        if (!is_dir($aimDir)) {            return false;        }        $dirHandle = opendir($aimDir);        while(false !== ($file = readdir($dirHandle))) {            if ($file == '.' || $file == '..') {                continue;            }            if (!is_dir($aimDir.$file)) {                FileUtil::unlinkFile($aimDir . $file);            } else {                FileUtil::unlinkDir($aimDir . $file);            }        }        closedir($dirHandle);        return rmdir($aimDir);    }// 删除文件    function unlinkFile($aimUrl) {        if (file_exists($aimUrl)) {            unlink($aimUrl);            return true;        } else {            return false;        }    }// 复制文件夹    function copyDir($oldDir, $aimDir, $overWrite = false) {        $aimDir = str_replace('', '/', $aimDir);        $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/';        $oldDir = str_replace('', '/', $oldDir);        $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir.'/';        if (!is_dir($oldDir)) {            return false;        }        if (!file_exists($aimDir)) {            FileUtil::createDir($aimDir);        }        $dirHandle = opendir($oldDir);        while(false !== ($file = readdir($dirHandle))) {            if ($file == '.' || $file == '..') {                continue;            }            if (!is_dir($oldDir . $file)) {                FileUtil::copyFile($oldDir . $file, $aimDir . $file, $overWrite);            } else {                FileUtil::copyDir($oldDir . $file, $aimDir . $file, $overWrite);            }        }        return closedir($dirHandle);    }//复制文件    function copyFile($fileUrl, $aimUrl, $overWrite = false) {        if (!file_exists($fileUrl)) {            return false;        }        if (file_exists($aimUrl) && $overWrite == false) {            return false;        } elseif (file_exists($aimUrl) && $overWrite == true) {            FileUtil::unlinkFile($aimUrl);        }        $aimDir = dirname($aimUrl);        FileUtil::createDir($aimDir);        copy($fileUrl, $aimUrl);        return true;    }}?>
Copy after login

“PHP文件操作类”的更多相关文章 》

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/335093.html pageNo:10
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

See all articles