/**
- * 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);
- $result = true;
- foreach ($arr as $str) {
- $aimDir. = $str. '/';
- if (!file_exists ($aimDir)) {
- $result = mkdir ($aimDir);
- }
- }
- return $result;
- }
/**
- * 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;
- }
- }
- ?>
Copy CodeAnother method of invocation (non-static invocation):
- $fu = new Fileutil ();
- $fu->copyfile (' A/1/2/3 ', ' A/1/2/4 ');
Copy Code |