PHP operation File Class function code (file and folder creation, copying, moving and deleting _ PHP-php Tutorial
Last Update:2017-05-14
Source: Internet
Author: User
PHP file operations (create, copy, move, and delete files and folders) are easy to use. For more information, see.
The code is as follows:
/**
* File operations
*
* Example:
* FileUtil: createDir ('a/1/2/3'); create a folder named a/1/2/3
* FileUtil: createFile ('B/1/2/3'); create a 3 file under the B/1/2/folder in the test.
* FileUtil: createFile ('B/1/2/3.exe'); create a 3.exe file under B/1/2/folder.
* FileUtil: copyDir ('B', 'd/e'); create a d/e folder in the copy folder and copy the content in the B folder.
* FileUtil: copyFile ('B/1/2/3.exe', 'B/B/3.exe'); create a B/B folder for the copied file, copy the 3.exe file in the B/1/2 folder.
* FileUtil: moveDir ('a/',' B/C'); test the mobile folder to create a B/c folder and move the content in folder, and delete the folder.
* FileUtil: moveFile ('B/1/2/3.exe', 'B/d/3.exe'); test to create a B/d folder for a mobile file, and move 3.exe in B/1/2
* FileUtil: unlinkFile ('B/d/3.exe'); test deleting a file B/d/3.exe
* FileUtil: unlinkDir ('D'); test the function of deleting a folder and deleting a 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 );
}
}
}
/**
* Create a file
*
* @ Param string $ aimUrl
* @ Param boolean $ overWrite this parameter controls whether to overWrite the original file
* @ 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 a folder
*
* @ Param string $ oldDir
* @ Param string $ aimDir
* @ Param boolean $ overWrite this parameter controls whether to overWrite the original file
* @ 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 a file
*
* @ Param string $ fileUrl
* @ Param string $ aimUrl
* @ Param boolean $ overWrite this parameter controls whether to overWrite the original file
* @ 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 a 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 an object
*
* @ Param string $ aimUrl
* @ Return boolean
*/
Function unlinkFile ($ aimUrl ){
If (file_exists ($ aimUrl )){
Unlink ($ aimUrl );
Return true;
} Else {
Return false;
}
}
/**
* Copy a folder
*
* @ Param string $ oldDir
* @ Param string $ aimDir
* @ Param boolean $ overWrite this parameter controls whether to overWrite the original file
* @ 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 an object
*
* @ Param string $ fileUrl
* @ Param string $ aimUrl
* @ Param boolean $ overWrite this parameter controls whether to overWrite the original file
* @ 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 call method:
The code is as follows:
$ Fu = new FileUtil ();
$ Fu-> copyFile ('a/1/2/3', 'A/1/2/4 ');