许久未学习了,难得抽闲,赶紧把这段时间新用到的方法整理下。
基于PHP7 以上的版本,自带的ZipArchive类 (开启ext-zip)扩展
class uploadZip
{
/**将本地文件上传至指定文件服务器并返回新的地址
* @param $file_url //绝对路径
* @param $path //要保存的目录
* @param $file_name //文件名
* @return bool
*/
public static function getUploadurl($file_url, $path, $file_name)
{
$error_msg = '';
try {
file_get_contents('远程文件的访问路径/' . $path . $file_name);
} catch (\Exception $e) {
$error_msg = $e->getMessage();
}
if (!$error_msg) {
//文件已存在
return $path . $file_name;
}
$http_url = 'file/contract';
$data = [
'image_info' => file_get_contents($file_url),
'path' => $path,
'file_name' => $file_name
];
//像要上传的地方提交要保存的路径、文件名、文件字节流
self:: curl_post($data);
}
/**将一堆文件打包成一个压缩包
* @param array $files //要添加的文件地址集合
* @param $obj_id //文件夹分类序号
* @return array
*/
public static function zipFilePack($files = array(), $obj_id = 0)//文件压缩打包
{
if (empty($files)) {
$result = [
'status' => -200,
'msg' => '无要压缩的文件',
'data' => ['zip_url' => ''],
];
return $result;
}
if ($obj_id) {
$zip_url = '/' . self::getUploadFullPath('', $obj_id) . date('YmdHis') . mt_rand(1000, 9999) . '.zip';;
}
$saveZipName = $_SERVER['DOCUMENT_ROOT'] . $zip_url;
$zip = new ZipArchive();
// 使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
if ($zip->open($saveZipName, ZipArchive::OVERWRITE) !== true) {
//OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
if ($zip->open($saveZipName, ZipArchive::CREATE) !== true) {
// 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
$result = [
'status' => -200,
'msg' => '无法打开文件,或者文件创建失败',
'data' => ['zip_url' => ''],
];
return $result;
}
}
//添加到压缩包
foreach ($files as $fileUrl) {
$fileUrl = $_SERVER['DOCUMENT_ROOT'] . '/' . $fileUrl;
if (file_exists($fileUrl) && is_file($fileUrl)) {
$zip->addFile($fileUrl, basename($fileUrl));
}
}
// 关闭
$zip->close();
$result = [
'status' => 200,
'msg' => '压缩成功',
'data' => ['zip_url' => $zip_url],
];
return $result;
}
/**将一个文件夹,保持其其内部排版内容,统一打包
* @param $obj_id
* @return array
*/
public static function zipDir($obj_id)
{
$zip_name = date('YmdHis') . mt_rand(1000, 9999) . '.zip';
if ($obj_id) {
$zip_url = '/Zip/' . $obj_id . '/';
} else {
$zip_url = '/Zip/';
}
$sub_dir = '/';
$zip_path = $_SERVER['DOCUMENT_ROOT'] . $zip_url;
$saveZipName = $zip_path . $zip_name;
$zip = new ZipArchive();
// 使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
if ($zip->open($saveZipName, ZipArchive::OVERWRITE) !== true) {
//OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
if ($zip->open($saveZipName, ZipArchive::CREATE) !== true) {
// 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
$result = [
'status' => -200,
'msg' => '无法打开文件,或者文件创建失败',
'data' => ['zip_url' => ''],
];
return $result;
}
}
self::addFileToZip($zip_path . 'data/', $zip, $sub_dir);
// 关闭
$zip->close();
self::deldir($zip_path . 'data/');
$result = [
'status' => 200,
'msg' => '压缩成功',
'data' => ['zip_url' => $zip_url . $zip_name],
];
return $result;
}
/**
* 创建目录
* @param $dir
* @param int $mode
* @return bool
*/
public static function createDirectory($dir, $mode = 0777)
{
if (is_dir($dir) || @mkdir($dir, $mode)) {
return true;
}
if (!mkdir(dirname($dir), $mode, true)) {
return false;
}
return @mkdir($dir, $mode);
}
/**移动文件 将制定路径文件$source 归整移动到 $dir 方便安装排版统一打包
* @param $source
* @param $dir
* @param $obj_id
* @return string
*/
public static function moveFile($source, $dir, $obj_id = 0)
{
$file_name = pathinfo($source, PATHINFO_BASENAME);
$source = $_SERVER['DOCUMENT_ROOT'] . '/' . $source;
if ($obj_id) {
$fullPath = $_SERVER['DOCUMENT_ROOT'] . '/Zip/' . $obj_id . '/data/' . $dir . '/';
} else {
$fullPath = $_SERVER['DOCUMENT_ROOT'] . '/Zip/' . $dir . '/';
}
//如果文件夹不存在则创建
if (!file_exists($fullPath)) {
self::createDirectory($fullPath, 0755);
}
$destination = $fullPath . $file_name;
$error_msg = '';
try {
copy($source, $destination);
} catch (\Exception $e) {
$error_msg = $e->getMessage();
}
return $error_msg;
}
/**删除目录
* @param $dir
* @return bool
*/
public static function deldir($dir)
{
//先删除目录下的文件:
$dh = opendir($dir);
while ($file = readdir($dh)) {
if ($file != "." && $file != "..") {
$fullpath = $dir . "/" . $file;
if (!is_dir($fullpath)) {
unlink($fullpath);
} else {
self:: deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if (rmdir($dir)) {
return true;
} else {
return false;
}
}
/**CURL
* @param $url
* @param $data
* @param int $timeout
* @param array $headers
* @return bool|string
*/
public static function curlPost($url, $data, $timeout = 10, $headers = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, "http://" . explode('/', $url)[2] . "/");
if ($data) {
curl_setopt($ch, CURLOPT_POST, 1);
}
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if ($headers) {
$headerArr = array();
foreach ($headers as $n => $v) {
$headerArr[] = $n . ':' . $v;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
curl_setopt($ch, CURLOPT_HEADER, 1);
} else {
curl_setopt($ch, CURLOPT_HEADER, 0);
}
if (stripos($url, 'https://') !== false) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
/**要上传的路径
* @param $rootDir
* @param int $obj_id
* @return string
*/
private static function getUploadFullPath($rootDir, $obj_id = 0)
{
if ($rootDir && !$obj_id) {
$fullPath = $rootDir;
} else {
$fullPath = 'uploads/data/' . $obj_id . '/' . date('Ym') . '/';
}
//如果文件夹不存在则创建
if (!file_exists($fullPath)) {
self::createDirectory($fullPath, 0755);
}
return $fullPath;
}
/**
* @param $path
* @param $zip
* @param $sub_dir
*/
private static function addFileToZip($path, &$zip, $sub_dir = '')
{
$handler = opendir($path);
while (($filename = readdir($handler)) !== false) {
if ($filename != "." && $filename != "..") {
if (is_dir($path . "/" . $filename)) {
$localPath = $sub_dir . $filename . '/'; //关键在这里,需要加上上一个递归的子目录
self::addFileToZip($path . "/" . $filename, $zip, $localPath);
} else { //将文件加入zip对象
$zip->addFile($path . "/" . $filename, $sub_dir . $filename);
}
}
}
@closedir($path);
}
}