zoukankan      html  css  js  c++  java
  • tp5上传压缩包到相应文件并自动解压到相应文件下

    <?php
    namespace appadmincontrollerupload;

    use appcommoncontrollerBackend;
    use thinkdb;
    use thinkRequest;
    use ZipArchive;
    /**
    * 上传功能
    *
    * @icon fa fa-user
    */
    class Upload extends Backend{
    protected $relationSearch = true;
    public $file_path1 = "uploads/publishh.zip";
    public $file_path2 = "E:PHPTutorialWWWpublishh";
    public $file_path3 = "E:PHPTutorialWWW";
    /**
    * 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());
    }
    }
    }
    }
  • 相关阅读:
    理解消息循环和窗口过程(转)
    对话框和控件编程(转)
    俄罗斯方块
    男生女生配(抽屉原理)
    翻转吧,字符串
    数塔
    Pseudoprime numbers伪素数(快速幂+判定素数)
    shǎ崽 OrOrOrOrz
    As Easy As A+B
    求素数(筛选法)
  • 原文地址:https://www.cnblogs.com/xieqijiang/p/10608110.html
Copyright © 2011-2022 走看看