PHP some well-packaged functions for folders

Source: Internet
Author: User

/** * Check if file name is valid * @param string $filename * @return Boolean*/functionCheckfilename ($filename){    $pattern= "/[\/,\*,<>,\?\|] /"; if(Preg_match($pattern,$filename )) {        return false; }Else{        return true; }}

<?PHP//Open the specified directory/** * Traverse directory functions to read only the outermost contents of the directory * @param string $path * @return Array*/functionReaddirectory ($path) {    $handle=Opendir($path );  while( ($item=Readdir($handle)) !==false ) {        //. and.. These 2 special directories        if($item! = "." &&$item!= "..") {            if(Is_file($path. "/" .$item )) {                $arr[' File '] [] =$item; }            if(Is_dir($path. "/" .$item )) {                $arr[' dir '] [] =$item; }                }    }    Closedir($handle ); return $arr;}//$path = "file";//print_r (Readdirectory ($path));/** * Get folder Size * @param string $path * @return int*/functionDirsize ($path){    $sum=0; Global $sum; $handle=Opendir($path);  while(($item=Readdir($handle))!==false){        if($item!="." &&$item!=".."){            if(Is_file($path." /".$item)){                $sum+=filesize($path." /".$item); }            if(Is_dir($path." /".$item)){                $func=__function__; $func($path." /".$item); }        }            }    Closedir($handle); return $sum;}//$path = "file";//echo dirsize ($path);functionCreateFolder ($dirname){    //checking the legality of folder names    if(Checkfilename (basename($dirname))){        //whether the folder name exists under the current directory with the same names        if(!file_exists($dirname)){            if(mkdir($dirname, 0777,true)){                $mes= "Folder creation succeeded"; }Else{                $mes= "Folder creation failed"; }        }Else{            $mes= "Same folder name exists"; }    }Else{        $mes= "Illegal folder name"; }    return $mes;}/** * Rename folder * @param string $oldname * @param string $newname * @return String*/functionRenamefolder ($oldname,$newname){    //checking the legality of folder names    if(Checkfilename (basename($newname))){        //detects if there is a folder name with the same names in the current directory        if(!file_exists($newname)){            if(Rename($oldname,$newname)){                $mes= "Rename succeeded"; }Else{                $mes= "Rename Failed"; }        }Else{            $mes= "There is a folder with the same name"; }    }Else{        $mes= "Illegal folder name"; }    return $mes;}functionCopyFolder ($src,$DST){    //echo $src, "---", $dst. " ----";    if(!file_exists($DST)){        mkdir($DST, 0777,true); }    $handle=Opendir($src);  while(($item=Readdir($handle))!==false){        if($item!="." &&$item!=".."){            if(Is_file($src." /".$item)){                Copy($src." /".$item,$DST." /".$item); }            if(Is_dir($src." /".$item)){                $func=__function__; $func($src." /".$item,$DST." /".$item); }        }    }    Closedir($handle); return"Copy succeeded"; }/** * Cut folder * @param string $src * @param string $DST * @return String*/functionCutfolder ($src,$DST){    //Echo $src, "--", $DST;    if(file_exists($DST)){        if(Is_dir($DST)){            if(!file_exists($DST." /".basename($src))){                if(Rename($src,$DST." /".basename($src))){                    $mes= "Cut Successfully"; }Else{                    $mes= "Cut Failed"; }            }Else{                $mes= "There is a folder with the same name"; }        }Else{            $mes= "Not a folder"; }    }Else{        $mes= "Destination folder does not exist"; }    return $mes;}/** * Delete folder * @param string $path * @return String*/functionDelfolder ($path){    $handle=Opendir($path);  while(($item=Readdir($handle))!==false){        if($item!="." &&$item!=".."){            if(Is_file($path." /".$item)){                unlink($path." /".$item); }            if(Is_dir($path." /".$item)){                $func=__function__; $func($path." /".$item); }        }    }    Closedir($handle); rmdir($path); return"Folder deletion succeeded";}

PHP some well-packaged functions for folders

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.