PHP action File class function code (file and folder creation, copy, move and delete) _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/**
* Manipulating file Classes
*
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 build file Build a 3.exe file under the b/1/2/folder
* Fileutil::copydir (' B ', ' d/e '); Test the Replication folder create a d/e folder and copy the contents of the B folder
* 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
* Fileutil::movedir (' A ', ' b/c '); Test the Move folder to create a b/c folder and move the contents of the a folder into the folder and delete the A folders
* 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
* Fileutil::unlinkfile (' b/d/3.exe '); Test Delete file Delete b/d/3.exe file
* Fileutil::unlinkdir (' d '); Test Delete folder Delete D folder
*/
Class Fileutil {
/**
* Create a folder
*
* @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);
}
}
}
/**
* Document Creation
*
* @param string $AIMURL
* @param boolean $overWrite This parameter controls whether the original file is overwritten
* @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;
}
/**
* Move Folder
*
* @param string $oldDir
* @param string $aimDir
* @param boolean $overWrite This parameter controls whether the original file is overwritten
* @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);
}
/**
* Move Files
*
* @param string $FILEURL
* @param string $AIMURL
* @param boolean $overWrite This parameter controls whether the original file is overwritten
* @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;
}
/**
* Delete Folder
*
* @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);
}
/**
* Delete Files
*
* @param string $AIMURL
* @return Boolean
*/
function Unlinkfile ($AIMURL) {
if (file_exists ($AIMURL)) {
Unlink ($AIMURL);
return true;
} else {
return false;
}
}
/**
* Copy Folder
*
* @param string $oldDir
* @param string $aimDir
* @param boolean $overWrite This parameter controls whether the original file is overwritten
* @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);
}
/**
* Copy Files
*
* @param string $FILEURL
* @param string $AIMURL
* @param boolean $overWrite This parameter controls whether the original file is overwritten
* @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;
}
}
?>

Another way to call:
Copy CodeThe code is as follows:
$fu = new Fileutil ();
$fu->copyfile (' A/1/2/3 ', ' A/1/2/4 ');

http://www.bkjia.com/PHPjc/324574.html www.bkjia.com true http://www.bkjia.com/PHPjc/324574.html techarticle Copy the code as follows:?/** * Manipulate 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 ') ...

  • 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.