tp5上传压缩包到相应文件并自动解压到相应文件下
<?php
namespace app\admin\controller\upload; use app\common\controller\Backend;
use think\db;
use think\Request;
use ZipArchive;
/**
* 上传功能
*
* @icon fa fa-user
*/
class Upload extends Backend{
protected $relationSearch = true;
public $file_path1 = "uploads/publishh.zip";
public $file_path2 = "E:\PHPTutorial\WWW\publishh";
public $file_path3 = "E:\PHPTutorial\WWW";
/**
* User模型对象
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = model('Player');
}
// 文件上传表单
public function index(){
if(file_exists($this->file_path1)){
unlink($this->file_path1);
return $this->fetch();
}
else {
return $this->fetch();
}
}
//删除文件夹以及文件夹里面所有的文件
public function deldir($dir) {
//先删除目录下的文件:
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
$this->deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if(@rmdir($dir)) {
return true;
} else {
return false;
}
}
//解压一个压缩包
/**
* 解压zip文件到指定目录
* $filepath: 文件路径
* $extractTo: 解压路径
*/
public function dr_unZip($filepath,$extractTo) {
$zip = new ZipArchive;
$res = $zip->open($filepath);
if ($res === TRUE) {
//解压缩到$extractTo指定的文件夹
$zip->extractTo($extractTo);
$zip->close();
echo "success改正";
} else {
echo 'failed, code:' . $res;
}
}
//判断是否有子文件
public function hasFile($dirName) {
if(file_exists($dirName) && $handle = opendir($dirName)) {
while(false !== ($item = readdir($handle))) {
if($item!= "." && $item != ".."){
echo $item;
}
}
}
}
// 上传文件方法
public function upload(){
if(file_exists($this->file_path1)){ //包含需要先删除该wen
$unlink = unlink($this->file_path1);
if($unlink=true){
$file = request()->file('files');
if (empty($file)) {
$this->error('请选择上传文件');
}
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads','');
if ($info) {
$del = $this->deldir($this->file_path2);
if($del=true){
$row = $this->dr_unZip($this->file_path1,$this->file_path3);
if($row=true){
$this->success('文件上传成功');
echo $info->getFilename();
} }
else {
echo "删除a目录失败";
}
}
else {
$this->error($file->getError());
}
}
else {
echo "删除失败";
}
}
else { //不包含就不用删除了
$file = request()->file('files');
if (empty($file)) {
$this->error('请选择上传文件');
}
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads','');
if ($info) {
$del = $this->deldir($this->file_path2);
if($del=true){
$row = $this->dr_unZip($this->file_path1,$this->file_path3);
if($row=true){
$this->success('文件上传成功');
echo $info->getFilename();
} }
else {
echo "删除a目录失败";
}
}
else {
$this->error($file->getError());
}
}
}
}
tp5上传压缩包到相应文件并自动解压到相应文件下的更多相关文章
- zend framework将zip格式的压缩文件导入并解压到指定文件
html代码 <pre class="php" name="code"><fieldset> <legend>批量导入学生照 ...
- Linux文件压缩/打包/解压
在Linux日常维护中,经常需要备份同步一些比较重要的文件,而在传输过程中如果文件比较大往往会非常慢,而且还会非常占用空间,这时候就需要我们使用压缩工具对大文件进行压缩打包,下面我们来介绍一下常用的压 ...
- PHP上传压缩包并自解压方法
1.PHP上传压缩包并解压的大概流程: 普通上传功能->上传到服务器->加载系统组件->找到上传的文件并执行解压命令->成功解压到目录 2.php执行系统命令的几类函数: (1 ...
- PHP自动解压上传的rar文件
PHP自动解压上传的rar文件 浏览:383 发布日期:2015/07/20 分类:功能实现 关键字: php函数 php扩展 大家都知道php有个zip类可直接操作zip压缩文件,可是用户有时候 ...
- C# 上传RAR文件 解压 获取解压后的文件名称
此方法适用于C盘windows文件夹中有WinRAR.exe文件 if (fileExt.ToUpper() == ".RAR") { string zpath = Server. ...
- Java解压上传zip或rar文件,并解压遍历文件中的html的路径
1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception { HttpSession session = request.getSe ...
- 本地上传文件至服务器的技巧(linux文件压缩及解压文件)
linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...
- spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置
spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...
- Java压缩包解压到指定文件
在获得一个以Zip格式压缩的文件之后,需要将其进行解压缩,还原成压缩前的文件.若是使用Java自带的压缩工具包来实现解压缩文件到指定文件夹的功能,因为jdk提供的zip只能按UTF-8格式处理,而Wi ...
随机推荐
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.gaiay.business.helper.dao.LiveRegenrationRecordMapper.insert
原因分析: 字段名称.报名.类名 对应不上 ,比如colomn和property属性 反了.. 按以下步骤一一执行: 1:检查xml文件所在的package名称是否和interface对应的packa ...
- winform基础控件-例子学习
1.如图实现整数计算器 ComboBox控件: Items属性:添加集合中的项. this.comoper.Items.AddRange(new object[] { "+", & ...
- Django objects.all() ,objects.get() ,objects.filter()之间的区别
ret=UserInfo.objects.all() all返回的是QuerySet对象,程序并没有真的在数据库中执行SQL语句查询数据,但支持迭代,使用for循环可以获取数据. ret=UserIn ...
- d3js selections深入理解
D3 selections选择DOM元素以便可以对这些dom元素做相应的操作,比如:更改其style,修改其属性,执行data-join操作,或者插入.删除相应elements 比如,如果给定5个ci ...
- CMD命令行下编译.Net Visual Studio 项目
有时候我们需要编译.net 的sln解决方案,可是VS打开的速度太慢,可以用命令行进行代替,详细过程如下: 1.开始菜单——>Visual Studio 2017(根据你电脑上安装的VS版本来) ...
- WritePrivateProfileString、GetPrivateProfileString 读写配置文件
WritePrivateProfileString 写配置文件 BOOL WINAPI WritePrivateProfileString( _In_ LPCTSTR lpAppName, _In_ ...
- nmap速查表v1.0(中文版)
基本语法: #nmap [扫描方式] [命令选项] {目标} 扫描目标格式: IPv4 地址: 192.168.1.1IPv6 地址:AABB:CCDD::FF%eth0主机名:www.target. ...
- Spark SQL on Yarn-Cluster
Spark SQL 或者Hive SQL在yarn-client模式下运行正常,但是在yarn-cluster模式下总是报 status failed错误, 需要把$SPARK_HOME/lib/sp ...
- SVG中的元素属性
SVG attributes by category Animation event attributes onbegin, onend, onload, onrepeat Animation att ...
- Redux 源码解读 —— 从源码开始学 Redux
已经快一年没有碰过 React 全家桶了,最近换了个项目组要用到 React 技术栈,所以最近又复习了一下:捡起旧知识的同时又有了一些新的收获,在这里作文以记之. 在阅读文章之前,最好已经知道如何使用 ...