根目錄下/extend/org中的UeditorUpload.php文件
~~~
<?php
namespace org;
class UeditorUpload
{
private $fileField; //文件域名
private $file; //文件上傳對象
private $base64; //文件上傳對象
private $config; //配置信息
private $oriName; //原始文件名
private $fileName; //新文件名
private $fullName; //完整文件名,即從當前配置目錄開始的URL
private $filePath; //完整文件名,即從當前配置目錄開始的URL
private $fileSize; //文件大小
private $fileType; //文件類型
private $stateInfo; //上傳狀態信息,
private $stateMap = [ //上傳狀態映射表,國際化用戶需考慮此處數據的國際化
"SUCCESS", //上傳成功標記,在UEditor中內不可改變,否則flash判斷會出錯
"文件大小超出 upload_max_filesize 限制",
"文件大小超出 MAX_FILE_SIZE 限制",
"文件未被完整上傳",
"沒有文件被上傳",
"上傳文件為空",
"ERROR_TMP_FILE" => "臨時文件錯誤",
"ERROR_TMP_FILE_NOT_FOUND" => "找不到臨時文件",
"ERROR_SIZE_EXCEED" => "文件大小超出網站限制",
"ERROR_TYPE_NOT_ALLOWED" => "文件類型不允許",
"ERROR_CREATE_DIR" => "目錄創建失敗",
"ERROR_DIR_NOT_WRITEABLE" => "目錄沒有寫權限",
"ERROR_FILE_MOVE" => "文件保存時出錯",
"ERROR_FILE_NOT_FOUND" => "找不到上傳文件",
"ERROR_WRITE_CONTENT" => "寫入文件內容錯誤",
"ERROR_UNKNOWN" => "未知錯誤",
"ERROR_DEAD_LINK" => "鏈接不可用",
"ERROR_HTTP_LINK" => "鏈接不是http鏈接",
"ERROR_HTTP_CONTENTTYPE" => "鏈接contentType不正確",
"INVALID_URL" => "非法 URL",
"INVALID_IP" => "非法 IP"
];
/**
* 構造函數
* Uploader constructor.
* @param string $fileField 表單名稱
* @param array $config 配置項
* @param string $base64 是否解析base64編碼,可省略。若開啟,則$fileField代表的是base64編碼的字符串表單名
*/
public function __construct($fileField, $config, $base64 = "upload")
{
$this->fileField = $fileField;
$this->config = $config;
$this->base64 = $base64;
if ($base64 == "remote") {
$this->saveRemote();
} else if ($base64 == "base64") {
$this->upBase64();
} else {
$this->upFile();
}
}
/**
* 上傳文件的主處理方法
* @return mixed
*/
private function upFile()
{
if (!isset($_FILES[$this->fileField])) {
$postMaxSize = ini_get('post_max_size');
$uploadMaxFileSize = ini_get('upload_max_filesize');
$this->stateInfo = '$_FILES數組未接收到上傳文件數據,請檢查上傳文件大小是否超出PHP配置文件post_max_size、upload_max_filesize設置項,';
$this->stateInfo .= '當前post_max_size限制大小為' . $postMaxSize;
$this->stateInfo .= ',當前upload_max_filesize限制大小為' . $uploadMaxFileSize;
return;
}
$file = $this->file = $_FILES[$this->fileField];
if (!$file) {
$this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
return;
}
if ($this->file['error']) {
$this->stateInfo = $this->getStateInfo($file['error']);
return;
} else if (!file_exists($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND");
return;
} else if (!is_uploaded_file($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMPFILE");
return;
}
$this->oriName = $file['name'];
$this->fileSize = $file['size'];
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);
//檢查文件大小是否超出限制
if (!$this->checkSize()) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}
//檢查是否不允許的文件格式
if (!$this->checkType()) {
$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
return;
}
//創建目錄失敗
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
return;
} else if (!is_writeable($dirname)) {
$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
return;
}
//移動文件
if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移動失敗
$this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");
} else { //移動成功
$this->stateInfo = $this->stateMap[0];
}
}
/**
* 處理base64編碼的圖片上傳
* @return mixed
*/
private function upBase64()
{
$base64Data = $_POST[$this->fileField];
$img = base64_decode($base64Data);
$this->oriName = $this->config['oriName'];
$this->fileSize = strlen($img);
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);
//檢查文件大小是否超出限制
if (!$this->checkSize()) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}
//創建目錄失敗
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
return;
} else if (!is_writeable($dirname)) {
$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
return;
}
//移動文件
if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移動失敗
$this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
} else { //移動成功
$this->stateInfo = $this->stateMap[0];
}
}
/**
* 拉取遠程圖片
* @return mixed
*/
private function saveRemote()
{
$imgUrl = htmlspecialchars($this->fileField);
$imgUrl = str_replace("&", "&", $imgUrl);
//http開頭驗證
if (strpos($imgUrl, "http") !== 0) {
$this->stateInfo = $this->getStateInfo("ERROR_HTTP_LINK");
return;
}
preg_match('/(^https*:\/\/[^:\/]+)/', $imgUrl, $matches);
$host_with_protocol = count($matches) > 1 ? $matches[1] : '';
// 判斷是否是合法 url
if (!filter_var($host_with_protocol, FILTER_VALIDATE_URL)) {
$this->stateInfo = $this->getStateInfo("INVALID_URL");
return;
}
preg_match('/^https*:\/\/(.+)/', $host_with_protocol, $matches);
$host_without_protocol = count($matches) > 1 ? $matches[1] : '';
// 此時提取出來的可能是 ip 也有可能是域名,先獲取 ip
$ip = gethostbyname($host_without_protocol);
// 判斷是否是私有 ip
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
$this->stateInfo = $this->getStateInfo("INVALID_IP");
return;
}
//獲取請求頭并檢測死鏈
$heads = get_headers($imgUrl, 1);
if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
$this->stateInfo = $this->getStateInfo("ERROR_DEAD_LINK");
return;
}
//格式驗證(擴展名驗證和Content-Type驗證)
$fileType = strtolower(strrchr($imgUrl, '.'));
if (!in_array($fileType, $this->config['allowFiles']) || !isset($heads['Content-Type']) || !stristr($heads['Content-Type'], "image")) {
$this->stateInfo = $this->getStateInfo("ERROR_HTTP_CONTENTTYPE");
return;
}
//打開輸出緩沖區并獲取遠程圖片
ob_start();
$context = stream_context_create(
['http' => [
'follow_location' => false // don't follow redirects
]]
);
readfile($imgUrl, false, $context);
$img = ob_get_contents();
ob_end_clean();
preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);
$this->oriName = $m ? $m[1] : "";
$this->fileSize = strlen($img);
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);
//檢查文件大小是否超出限制
if (!$this->checkSize()) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}
//創建目錄失敗
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
return;
} else if (!is_writeable($dirname)) {
$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
return;
}
//移動文件
if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移動失敗
$this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
} else { //移動成功
$this->stateInfo = $this->stateMap[0];
}
}
/**
* 上傳錯誤檢查
* @param $errCode
* @return string
*/
private function getStateInfo($errCode)
{
return !$this->stateMap[$errCode] ? $this->stateMap["ERROR_UNKNOWN"] : $this->stateMap[$errCode];
}
/**
* 獲取文件擴展名
* @return string
*/
private function getFileExt()
{
return strtolower(strrchr($this->oriName, '.'));
}
/**
* 重命名文件
* @return string
*/
private function getFullName()
{
//替換日期事件
$t = time();
$d = explode('-', date("Y-y-m-d-H-i-s"));
$format = $this->config["pathFormat"];
$format = str_replace("{yyyy}", $d[0], $format);
$format = str_replace("{yy}", $d[1], $format);
$format = str_replace("{mm}", $d[2], $format);
$format = str_replace("{dd}", $d[3], $format);
$format = str_replace("{hh}", $d[4], $format);
$format = str_replace("{ii}", $d[5], $format);
$format = str_replace("{ss}", $d[6], $format);
$format = str_replace("{time}", $t, $format);
//過濾文件名的非法自負,并替換文件名
$oriName = substr($this->oriName, 0, strrpos($this->oriName, '.'));
$oriName = preg_replace("/[\|\?\"\<\>\/\*\\\\]+/", '', $oriName);
$format = str_replace("{filename}", $oriName, $format);
//替換隨機字符串
$randNum = rand(1, 10000000000) . rand(1, 10000000000);
if (preg_match("/\{rand\:([\d]*)\}/i", $format, $matches)) {
$format = preg_replace("/\{rand\:[\d]*\}/i", substr($randNum, 0, $matches[1]), $format);
}
$ext = $this->getFileExt();
return $format . $ext;
}
/**
* 獲取文件名
* @return string
*/
private function getFileName()
{
return substr($this->filePath, strrpos($this->filePath, '/') + 1);
}
/**
* 獲取文件完整路徑
* @return string
*/
private function getFilePath()
{
$fullname = $this->fullName;
$rootPath = $_SERVER['DOCUMENT_ROOT'];
if (substr($fullname, 0, 1) != '/') {
$fullname = '/' . $fullname;
}
return $rootPath . $fullname;
}
/**
* 文件類型檢測
* @return bool
*/
private function checkType()
{
return in_array($this->getFileExt(), $this->config["allowFiles"]);
}
/**
* 文件大小檢測
* @return bool
*/
private function checkSize()
{
return $this->fileSize <= ($this->config["maxSize"]);
}
/**
* 獲取當前上傳成功文件的各項信息
* @return array
*/
public function getFileInfo()
{
return [
"state" => $this->stateInfo,
"url" => $this->fullName,
"title" => $this->fileName,
"original" => $this->oriName,
"type" => $this->fileType,
"size" => $this->fileSize
];
}
}
~~~
- 心靈筆記
- tp5在編輯器一行一個添加信息
- 出庫入庫處理
- 出庫
- 入庫
- 后臺控制器
- tp5加載更多-流加載
- 配件列表
- Parts.php
- add.html
- edit.html
- 圖片管理
- controller
- 1.imgfile.php
- 2.imgfiletag.php
- view
- imgfile
- 1.index.html
- 2.show.html
- 3.simple.html
- 4.more.html
- 5.edit.html
- 6.deittag.html
- imgfiletag
- add.htm
- edit.htm
- model
- imgfile.php
- imgfiletag.php
- api
- upload.php
- js
- admin.js
- tp5整合百度編輯器多圖上傳
- 1.html代碼
- 2.js代碼
- 3.api代碼
- 4.extend中的拓展代碼
- tp5刪除圖片同時刪除服務器圖片
- 實用字段的的添加更新
- 根據時間戳獲取未來幾天星期幾
- curl
- 城市按字母排版
- 聚合查詢及渲染,處理產品首頁
- 過濾字段
- 風控
- 查詢id是否在數組里
- 判斷更新還是新增操作
- tp5使用引入php文件