[李景山php]每天TP5-20170104|thinkphp5-File.php-1

來源:互聯網
上載者:User

標籤:thinkphp5

<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <[email protected]>// +----------------------------------------------------------------------namespace think;use SplFileInfo;// Spl 檔案資訊 類庫use SplFileObject;// Spl 檔案對象 類庫// Spl 一個需要面對的問題class File extends SplFileObject{// 檔案 繼承 SplFileObject 對象    /**     * 錯誤資訊     * @var string     */    private $error = ‘‘;// 錯誤資訊儲存    // 當前完整檔案名稱    protected $filename;// 當前完整的檔案名稱 資訊    // 上傳檔案名稱    protected $saveName;// 上傳 檔案名稱    // 檔案上傳命名規則    protected $rule = ‘date‘;// 檔案上傳的 命名規則    // 檔案上傳驗證規則    protected $validate = [];// 檔案上傳 驗證規則    // 單元測試    protected $isTest;// 單元測試    // 上傳檔案資訊    protected $info;// 上傳檔案資訊    // 檔案hash資訊    protected $hash = [];// 檔案 hash 資訊    public function __construct($filename, $mode = ‘r‘)// 結構體    {        parent::__construct($filename, $mode);// 函數 構造體 父親的        $this->filename = $this->getRealPath();// 擷取 檔案名稱    }    /**     * 是否測試     * @param  bool   $test 是否測試     * @return $this     */    public function isTest($test = false)    {        $this->isTest = $test;// 是否測試資訊        return $this;    }    /**     * 設定上傳資訊     * @param  array   $info 上傳檔案資訊     * @return $this     */    public function setUploadInfo($info)    {        $this->info = $info;// 設定 上傳檔案資訊        return $this;    }    /**     * 擷取上傳檔案的資訊     * @param  string   $name     * @return array|string     */    public function getInfo($name = ‘‘)    {        return isset($this->info[$name]) ? $this->info[$name] : $this->info;// 擷取上傳檔案資訊    }    /**     * 擷取上傳檔案的檔案名稱     * @return string     */    public function getSaveName()    {        return $this->saveName;// 擷取儲存的檔案名稱    }    /**     * 設定上傳檔案的儲存檔案名稱     * @param  string   $saveName     * @return $this     */    public function setSaveName($saveName)// 設定儲存的檔案名稱    {        $this->saveName = $saveName;        return $this;    }    /**     * 擷取檔案的md5散列值     * @return $this     */    public function md5()// md5 加密檔案    {        if (!isset($this->hash[‘md5‘])) {            $this->hash[‘md5‘] = md5_file($this->filename);        }        return $this->hash[‘md5‘];    }    /**     * 擷取檔案的sha1散列值     * @return $this     */    public function sha1()// sha1 檔案 散列值    {        if (!isset($this->hash[‘sha1‘])) {            $this->hash[‘sha1‘] = sha1_file($this->filename);        }        return $this->hash[‘sha1‘];    }    /**     * 檢查目錄是否可寫     * @param  string   $path    目錄     * @return boolean     */    protected function checkPath($path)//目錄 檢測    {        if (is_dir($path)) {            return true;        }        if (mkdir($path, 0755, true)) {            return true;        } else {            $this->error = "目錄 {$path} 建立失敗!";            return false;        }    }    /**     * 擷取檔案類型資訊     * @return string     */    public function getMime()    {        $finfo = finfo_open(FILEINFO_MIME_TYPE);// 擷取檔案 類型        return finfo_file($finfo, $this->filename);    }    /**     * 設定檔案的命名規則     * @param  string   $rule    檔案命名規則     * @return $this     */    public function rule($rule)    {// 規則設定        $this->rule = $rule;        return $this;    }    /**     * 設定上傳檔案的驗證規則     * @param  array   $rule    驗證規則     * @return $this     */    public function validate($rule = [])    {// 驗證規則        $this->validate = $rule;        return $this;    }    /**     * 檢測是否合法的上傳檔案     * @return bool     */    public function isValid()    {// 檢測 檔案 是否 合法        if ($this->isTest) {            return is_file($this->filename);// 驗證是否 為檔案        }        return is_uploaded_file($this->filename);// 驗證 是否 為 上傳檔案    }


本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1877551

[李景山php]每天TP5-20170104|thinkphp5-File.php-1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.