Home Backend Development PHP Tutorial PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除)_php技巧

PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除)_php技巧

May 17, 2016 am 09:14 AM
File class

复制代码 代码如下:


/**
* 操纵文件类
*
* 例子:
* FileUtil::createDir('a/1/2/3'); 测试建立文件夹 建一个a/1/2/3文件夹
* FileUtil::createFile('b/1/2/3'); 测试建立文件 在b/1/2/文件夹下面建一个3文件
* FileUtil::createFile('b/1/2/3.exe'); 测试建立文件 在b/1/2/文件夹下面建一个3.exe文件
* FileUtil::copyDir('b','d/e'); 测试复制文件夹 建立一个d/e文件夹,把b文件夹下的内容复制进去
* FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe'); 测试复制文件 建立一个b/b文件夹,并把b/1/2文件夹中的3.exe文件复制进去
* FileUtil::moveDir('a/','b/c'); 测试移动文件夹 建立一个b/c文件夹,并把a文件夹下的内容移动进去,并删除a文件夹
* FileUtil::moveFile('b/1/2/3.exe','b/d/3.exe'); 测试移动文件 建立一个b/d文件夹,并把b/1/2中的3.exe移动进去
* FileUtil::unlinkFile('b/d/3.exe'); 测试删除文件 删除b/d/3.exe文件
* FileUtil::unlinkDir('d'); 测试删除文件夹 删除d文件夹
*/
class FileUtil {
/**
* 建立文件夹
*
* @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);
}
}
}
/**
* 建立文件
*
* @param string $aimUrl
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @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;
}
/**
* 移动文件夹
*
* @param string $oldDir
* @param string $aimDir
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @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);
}
/**
* 移动文件
*
* @param string $fileUrl
* @param string $aimUrl
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @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;
}
/**
* 删除文件夹
*
* @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);
}
/**
* 删除文件
*
* @param string $aimUrl
* @return boolean
*/
function unlinkFile($aimUrl) {
if (file_exists($aimUrl)) {
unlink($aimUrl);
return true;
} else {
return false;
}
}
/**
* 复制文件夹
*
* @param string $oldDir
* @param string $aimDir
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @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);
}
/**
* 复制文件
*
* @param string $fileUrl
* @param string $aimUrl
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @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;
}
}
?>

另一种调用方式:
复制代码 代码如下:

$fu = new FileUtil();
$fu->copyFile('a/1/2/3', 'a/1/2/4');
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

How to Register and Use Laravel Service Providers How to Register and Use Laravel Service Providers Mar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

See all articles