<?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上传压缩包到相应文件并自动解压到相应文件下的更多相关文章

  1. zend framework将zip格式的压缩文件导入并解压到指定文件

    html代码 <pre class="php" name="code"><fieldset> <legend>批量导入学生照 ...

  2. Linux文件压缩/打包/解压

    在Linux日常维护中,经常需要备份同步一些比较重要的文件,而在传输过程中如果文件比较大往往会非常慢,而且还会非常占用空间,这时候就需要我们使用压缩工具对大文件进行压缩打包,下面我们来介绍一下常用的压 ...

  3. PHP上传压缩包并自解压方法

    1.PHP上传压缩包并解压的大概流程: 普通上传功能->上传到服务器->加载系统组件->找到上传的文件并执行解压命令->成功解压到目录 2.php执行系统命令的几类函数: (1 ...

  4. PHP自动解压上传的rar文件

    PHP自动解压上传的rar文件   浏览:383 发布日期:2015/07/20 分类:功能实现 关键字: php函数 php扩展 大家都知道php有个zip类可直接操作zip压缩文件,可是用户有时候 ...

  5. C# 上传RAR文件 解压 获取解压后的文件名称

    此方法适用于C盘windows文件夹中有WinRAR.exe文件 if (fileExt.ToUpper() == ".RAR") { string zpath = Server. ...

  6. Java解压上传zip或rar文件,并解压遍历文件中的html的路径

    1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception { HttpSession session = request.getSe ...

  7. 本地上传文件至服务器的技巧(linux文件压缩及解压文件)

    linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...

  8. spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

    spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...

  9. Java压缩包解压到指定文件

    在获得一个以Zip格式压缩的文件之后,需要将其进行解压缩,还原成压缩前的文件.若是使用Java自带的压缩工具包来实现解压缩文件到指定文件夹的功能,因为jdk提供的zip只能按UTF-8格式处理,而Wi ...

随机推荐

  1. Spring Boot框架搭建

    用idea搭建Springboot还是很方便的 环境变量是JDK1.8 SpringBoot自带了Tomcat启动也很方便 1.创建项目 2. 3.选择SpringBoot的版本以及组件 4.创建完成 ...

  2. styling the SVG images

    SVG不像canvas,SVG的所有元素都是以DOM元素存在于文档中的,我们可以像给任何普通的dom元素添加css样式一样,可以对svg的元素做styling.不过SVG元素的css样式名称和普通ht ...

  3. [翻译] TLTagsControl

    TLTagsControl https://github.com/ali312/TLTagsControl#tltagscontrol A nice and simple tags input con ...

  4. CSV输入输出

    读取csv文件: import csv cf = open('D:\pywe.csv','rb') cf.readline() #读取标题行,光标移动到下一行(相当于调过标题行) for l in c ...

  5. yum 安装 lnmp

    一. 系统 更新 yum -y update 二. 安装nginx 创建文件 vim /etc/yum.repos.d/nginx.repo 文件内容,这配置是安装最新的稳定版1.8 [nginx] ...

  6. mysql 基础学习1

    安装得方法有很多,这里就不详细介绍了. 进入 mysql 控制台 mysql -uroot -p 查看 有哪些库 show databases; 1.创建一个库 create database tes ...

  7. ZT 理解class.forName()

    理解class.forName() 分类: [Java SE 基础] 2012-05-17 07:53 3892人阅读 评论(3) 收藏 举报 classloaderclassjdbcejb数据库 目 ...

  8. python将字符串转变成dict格式

    字符串的内容是字典,需将字符串转变成字典格式 s1 = '{"lid":2,"date":"20190211","type&quo ...

  9. Angular Reactive Form-响应式表单验证

    内建验证规则 Angular中提供了一些內建的Validators,这些验证规则可以在Template-Driven或Reactive表单中使用. 目前 Angular 支持的内建 validator ...

  10. vim在插入模式粘贴代码缩进问题解决方法

    转载自:https://blog.csdn.net/commshare/article/details/6215088 在vim粘贴代码会出现缩进问题,原因在于vim在代码粘贴时会自动缩进 解决方法: ...