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 app\controllers;

use yii\web\Controller;
use app\models\FetchFiles;

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 app\models;


use yii\base\Model;

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文档

 

posted @ 2016-11-24 23:28  简庆旺  阅读(1413)  评论(2编辑  收藏  举报