Show all files and folders php file and folder operations (create, delete, move, copy)

Source: Internet
Author: User
Create the fileutil.php file with the following content and invocation methods:

 Php/** * manipulating File class * * Example: * Fileutil::createdir (' A/1/2/3 ');                    Test build folder Build a A/1/2/3 folder * fileutil::createfile (' B/1/2/3 ');             Test build file Build a 3 file under the b/1/2/folder * fileutil::createfile (' b/1/2/3.exe ');                    Test setup file build a 3.exe file under the b/1/2/folder * Fileutil::copydir (' B ', ' d/e '); Test the Copy folder to create a d/e folder, copy the contents of the B folder into the Fileutil::copyfile (' B/1/2/3.exe ', ' b/b/3.exe ');                    Test the copy file to create a b/b folder and copy the 3.exe files in the B/1/2 folder into the * Fileutil::movedir (' A/', ' b/c '); Test the Mobile folder to create a b/c folder, and move the contents of the A folder into, and delete the A folder * Fileutil::movefile (' b/1/2/3.exe ', ' b/d/3.exe ');             Test the move file to create a b/d folder and move the 3.exe in the B/1/2 into the * fileutil::unlinkfile (' b/d/3.exe ');                      Test Delete File Delete b/d/3.exe file * Fileutil::unlinkdir (' d '); Test Delete folder Delete D folder*/classFileutil {/** * Create Folder * * @param string $AIMURL * @return Viod*/functionCreatedir ($AIMURL) {        $AIMURL=Str_replace('', '/',$AIMURL); $aimDir= ''; $arr=Explode('/',$AIMURL); $result=true; foreach($arr as$str) {            $aimDir.=$str. '/'; if(!file_exists($aimDir)) {                $result=mkdir($aimDir); }        }        return$result; }    /** * Create File * * @param string $AIMURL * @param boolean $overWrite This parameter controls whether to overwrite the original file * @return Boolean */functionCreateFile ($AIMURL,$overWrite=false) {        if(file_exists($AIMURL) &&$overWrite==false) {            returnfalse; } ElseIf(file_exists($AIMURL) &&$overWrite==true) {Fileutil:: Unlinkfile ($AIMURL); }        $aimDir=dirname($AIMURL); Fileutil:: Createdir ($aimDir); Touch($AIMURL); returntrue; }    /** * Move Folder * * @param string $oldDir * @param string $aimDir * @param boolean $overWrite This parameter controls whether to overwrite Original file * @return Boolean*/functionMovedir ($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)) {            returnfalse; }        if(!file_exists($aimDir) ) {Fileutil:: Createdir ($aimDir); }        @ $dirHandle=Opendir($oldDir); if(!$dirHandle) {            returnfalse; }         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); returnrmdir($oldDir); }    /** * Move File * * @param string $FILEURL * @param string $aimUrl * @param boolean $overWrite This parameter controls whether to overwrite Original file * @return Boolean*/functionMoveFile ($FILEURL,$AIMURL,$overWrite=false) {        if(!file_exists($FILEURL)) {            returnfalse; }        if(file_exists($AIMURL) &&$overWrite=false) {            returnfalse; } ElseIf(file_exists($AIMURL) &&$overWrite=true) {Fileutil:: Unlinkfile ($AIMURL); }        $aimDir=dirname($AIMURL); Fileutil:: Createdir ($aimDir); Rename($FILEURL,$AIMURL); returntrue; }    /** * Delete folder * * @param string $aimDir * @return Boolean*/functionUnlinkdir ($aimDir) {        $aimDir=Str_replace('', '/',$aimDir); $aimDir=substr($aimDir,-1) = = '/'?$aimDir:$aimDir. '/'; if(!Is_dir($aimDir)) {            returnfalse; }        $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); returnrmdir($aimDir); }    /** * Delete file * * @param string $AIMURL * @return Boolean*/functionUnlinkfile ($AIMURL) {        if(file_exists($AIMURL)) {            unlink($AIMURL); returntrue; } Else {            returnfalse; }    }    /** * Copy Folder * * @param string $oldDir * @param string $aimDir * @param boolean $overWrite This parameter controls whether to overwrite Original file * @return Boolean*/functionCopydir ($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)) {            returnfalse; }        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); }        }        returnClosedir($dirHandle); }    /** * Copy file * * @param string $FILEURL * @param string $aimUrl * @param boolean $overWrite This parameter controls whether to overwrite Original file * @return Boolean*/functionCopyFile ($FILEURL,$AIMURL,$overWrite=false) {        if(!file_exists($FILEURL)) {            returnfalse; }        if(file_exists($AIMURL) &&$overWrite==false) {            returnfalse; } ElseIf(file_exists($AIMURL) &&$overWrite==true) {Fileutil:: Unlinkfile ($AIMURL); }        $aimDir=dirname($AIMURL); Fileutil:: Createdir ($aimDir); Copy($FILEURL,$AIMURL); returntrue; }}?>

Another way to call:

$fu New fileutil (); $fu->copyfile (' A/1/2/3 ', ' A/1/2/4 ');

Recommend a free time development of the web search engine, 360 disk search (www.360panso.com)

The above describes the display of all files and folders php file and folder operations (create, delete, move, copy), including the display of all files and folders content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.