分享一个很实用的php文件复制,下载、解压、删除可批量综合操作类
全部都是静态方法,不需要实例化即可调用
代码如下:
<?php
!defined('DS') && define('DS', DIRECTORY_SEPARATOR);
/**
* Name 文件下载、解压、复制、移动操作综合类
* Author 沉梦执于梦
*/
class File
{
public static $zipObj = null;
public function __construct()
{
return true;
}
/**
* 远程文件下载
* @param string $filePath 远程文件直链
* @param string $targetPath 本地目标路径
* @return string
*/
public static function copyWebFile($filePath = '', $targetPath = '')
{
//下载远程文件
if (function_exists("set_time_limit")) {
@set_time_limit(0);
}
if (function_exists("ignore_user_abort")) {
@ignore_user_abort(true);
}
$time = time();
$local = $targetPath;
ob_start(); //打开输出
// 分段读取文件
$header = get_headers($filePath, 1);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $local . '"');
header('Content-Length: ' . $header['Content-Length']);
flush();
readfile($filePath);
$filedata = ob_get_contents(); //得到浏览器输出
ob_clean(); //清空输出
header('content-Type:text/html;charset=utf-8');
if (file_put_contents($local, $filedata)) {
return true;
}
return false;
}
/**
* 解压ZIP文件
* @param string $src ZIP压缩文件
* @param string $dest 目标地址目录
* @return bool
*/
public static function zipExtract($src, $dest)
{
$zip = new \ZipArchive();
self::$zipObj = $zip;
if ($zip->open($src) === true) {
$zip->extractTo($dest);
$zip->close();
return true;
}
return false;
}
/**
* 打包一个目录到ZIP文件,支持多层
* @param string $dir 要压缩的文件来源目录
* @param string $filepath ZIP压缩文件路径,包含文件名
* @return bool
*/
public static function zipCreateDir($dir, $filepath)
{
$zip = new \ZipArchive();
self::$zipObj = $zip;
if ($zip->open($filepath, \ZipArchive::CHECKCONS) === true) {
if (self::zipCreate($zip, $dir)) {
return true;
}
throw new Exception("向压缩档添加压缩文件失败!");
return false;
} else {
throw new Exception("打开压缩文件" . $filepath . "失败!");
return false;
}
}
/**
* 打包创建当前目标和目录内的文件 可用于递归处理打包多个文件夹和文件夹内文件
* @param [type] $zipObj zip对象
* @param [type] $dir 目标
*/
private static function zipCreate($zipObj, $dirpath, $dirname = '')
{
$files = scandir($dirpath);
if (empty($dirname)) {
$dirname = $dirpath;
}
if (count($files) > 0) {
foreach ($files as $filename) {
if ($filename === "." || $filename === "..") {
continue;
}
if (is_dir($dirpath . $filename)) {
$zipObj->addEmptyDir($dirname . $filename);
self::zipCreate($zipObj, $dirname . $filename);
} else {
$zipObj->addFile($filename);
//$zipObj->addFile($dir . $filename);
}
}
return true;
}
return false;
}
/**
* 移动文件
* @param string $dir 文件来源地址
* @param string $newDir 文件目标地址
* @return bool
*/
public static function moveDir($dir = '', $newDir = '')
{
if (!is_dir($dir)) {
return false;
}
if (!is_dir($newDir)) {
@mkdir($newDir);
}
$dh = opendir($dir);
@chmod($dir, 0755);
@chmod($newDir, 0755);
while ($file = readdir($dh)) {
if ($file != "." && $file != "..") {
if (is_dir($dir . DS . $file)) {
self::moveDir($dir . DS . $file, $newDir . DS . $file);
} else {
$fullpath = $newDir . DS . $file;
$oldpath = $dir . DS . $file;
@chmod($fullpath, 0755);
@chmod($oldpath, 0755);
copy($oldpath, $fullpath);
unlink($oldpath);
}
}
}
closedir($dh);
if (rmdir($dir)) {
return true;
} else {
return false;
}
}
/**
* 批量删除文件
* @param string $dir 需要批量删除文件的目录
*/
public static function delFiles($dir)
{
$dir = str_replace('/', DS, $dir);
$dh = opendir($dir);
@chmod($dir, 0755);
while ($filename = readdir($dh)) {
if ($filename === "." || $filename === "..") {
continue;
}
if (is_dir($dir . $filename)) {
self::delFiles($dir . $filename . DS);
@rmdir($dir . $filename . DS);
} else {
@unlink($dir . $filename);
@rmdir($dir);
}
}
return true;
}
/**
* 验证文件目录 如果不存在就自动创建
* @param string $dir 目录
* @return bool
*/
public static function checkDir($dir = '')
{
if (empty($dir)) {
return false;
}
$dir = str_replace('/', DS, $dir);
if (!is_dir($dir)) {
return @mkdir($dir);
}
return true;
}
}-- 展开阅读全文 --
