zoukankan      html  css  js  c++  java
  • Yii2:避免文件路径暴漏,代理访问文件

    制作背景:公司要做第三方文件管理系统,客户有时候需要直接访问文件,但是我们又不想暴露文件路径,才有这代理访问

     

    基本功能介绍:读取txt文档、读取图片,如果有需要,可以通过插件读取doc、pdf文档,

    http://www.yii2.com/uploads/temp/read.bmp是我的真实路径

    控制器

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2016/11/24 0024
     * Time: 14:38
     */
    
    namespace appcontrollers;
    
    use yiiwebController;
    use appmodelsFetchFiles;
    
    class FetchFilesController extends Controller
    {
        public $file_path = 'http://www.yii2.com/uploads/temp/read.bmp';
        public function actionReadFile(){
            $file_path = $this->file_path;
           // echo  $file_path;
            //die;
            $FetchFiles = new FetchFiles();
          $FetchFiles->actionReadFile($file_path);
    
        }
    
    }

    模型代码

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2016/11/24 0024
     * Time: 16:53
     */
    
    namespace appmodels;
    
    
    use yiibaseModel;
    
    class FetchFiles extends Model
    {
    
    
        /**
         *转换路径为虚拟路径,返回给客户
         */
        public  function actionVirtualFile(){
            //virtualfile需要改为控制器名字,每次访问转换调用控制器去访问
            $file_path = $this->file_path;
            $file_path = str_replace('uploads/temp','virtualfile',$file_path);
            echo($file_path);
        }
    
    
    
    //客户访问资源时候,转换真实路径
        public function  actionReadFile($file_path){
    
            //获取真实资源路径
            $file_path = str_replace('virtualfile','uploads/temp',$file_path);
          //  Header("Location: $file_path");
            //die();
            //判断文件;类型
            $fileType =  substr(strrchr($file_path, '.'), 1);
            //统一转换为小写
            $fileType = strtolower($fileType);
            //选择文件类型,根据文件类型调用不同方法读取文件
            switch($fileType){
                case 'png':
                    $this->actionReadImg($file_path,$fileType);
                    break;
                case 'jpg':
                    $this->actionReadImg($file_path,$fileType);
                    break;
                case 'jpeg':
                    $this->actionReadImg($file_path,$fileType);
                    break;
                    break;
                case 'bmp':
                    $this->actionReadImg($file_path,$fileType);
                    break;
    
                case 'txt':
                    $this->actionReadTxt($file_path);
                    break;
    
                default:
                    echo  $fileType. "文件类型不支持查看,请直接下载!";
            }
            // echo $fileType;
            // echo file_get_contents("$file_path");
    
        }
    
        //读取txt文档的方法
        public function actionReadTxt($file_path){
            //echo '使用访问文件的方法'.$file_path;
          //  $content = file_get_contents($file_path);
            $handle = fopen("$file_path", 'r');
            $content = '';
            while(false != ($a = fread($handle, 8080))){//返回false表示已经读取到文件末尾
                $content .= $a;
            }
            fclose($handle);
            //转码,确保文档是utf-8;
            $content =  iconv('GB2312', 'UTF-8', $content);
           echo $content;
        }
    
        //读取图片的方法
        public function actionReadImg($file_path,$fileType){
    
            $contents=file_get_contents($file_path);
            //设置图片的头文件
            $header = 'Content-Type: image/'.$fileType;
            header( "$header" );//访问图片
            base64_decode($contents);
            echo $contents;
        }
    
    }

    效果展示:

    读取bmp后缀的图片

    读取txt文档

     

  • 相关阅读:
    SpringMVC初识视图解析器
    初识SpringMVC
    C++ 虚函数表
    C++ 纯虚函数 + 抽象类
    C++ 虚析构函数
    C++ 虚函数
    C++ 多态
    leetcode
    leetcode
    leetcode 10.正则表达式匹配
  • 原文地址:https://www.cnblogs.com/jianqingwang/p/6099996.html
Copyright © 2011-2022 走看看