修正request类的file方法

This commit is contained in:
thinkphp
2016-06-21 11:53:38 +08:00
parent 70f146f3e3
commit 2281f1edb4
2 changed files with 51 additions and 51 deletions

View File

@@ -48,7 +48,7 @@ class File extends SplFileObject
/** /**
* 设置上传信息 * 设置上传信息
* @param bool $info 上传文件信息 * @param array $info 上传文件信息
* @return $this * @return $this
*/ */
public function setUploadInfo($info) public function setUploadInfo($info)

View File

@@ -12,9 +12,9 @@
namespace think; namespace think;
use think\Config; use think\Config;
use think\Exception;
use think\File; use think\File;
use think\Session; use think\Session;
use think\Exception;
class Request class Request
{ {
@@ -82,7 +82,7 @@ class Request
protected $post = []; protected $post = [];
protected $request = []; protected $request = [];
protected $put; protected $put;
protected $delete; protected $delete;
protected $session = []; protected $session = [];
protected $file = []; protected $file = [];
protected $cookie = []; protected $cookie = [];
@@ -151,7 +151,7 @@ class Request
self::$hook = array_merge(self::$hook, $method); self::$hook = array_merge(self::$hook, $method);
} else { } else {
self::$hook[$method] = $callback; self::$hook[$method] = $callback;
} }
} }
/** /**
@@ -210,19 +210,19 @@ class Request
if (!isset($info['path'])) { if (!isset($info['path'])) {
$info['path'] = '/'; $info['path'] = '/';
} }
$options = []; $options = [];
$queryString = ''; $queryString = '';
if (isset($info['query'])) { if (isset($info['query'])) {
parse_str(html_entity_decode($info['query']), $query); parse_str(html_entity_decode($info['query']), $query);
if (!empty($params)) { if (!empty($params)) {
$params = array_replace($query, $params); $params = array_replace($query, $params);
$queryString = http_build_query($query, '', '&'); $queryString = http_build_query($query, '', '&');
} else { } else {
$params = $query; $params = $query;
$queryString = $info['query']; $queryString = $info['query'];
} }
} elseif (!empty($params)) { } elseif (!empty($params)) {
$queryString = http_build_query($params, '', '&'); $queryString = http_build_query($params, '', '&');
} }
$server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : ''); $server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
$server['QUERY_STRING'] = $queryString; $server['QUERY_STRING'] = $queryString;
@@ -232,7 +232,7 @@ class Request
$options['server'] = $server; $options['server'] = $server;
$options['url'] = $server['REQUEST_URI']; $options['url'] = $server['REQUEST_URI'];
$options['baseUrl'] = $info['path']; $options['baseUrl'] = $info['path'];
$options['pathinfo'] = ltrim($info['path'],'/'); $options['pathinfo'] = ltrim($info['path'], '/');
$options['method'] = $server['REQUEST_METHOD']; $options['method'] = $server['REQUEST_METHOD'];
$options['domain'] = $server['HTTP_HOST']; $options['domain'] = $server['HTTP_HOST'];
self::$instance = new self($options); self::$instance = new self($options);
@@ -394,15 +394,15 @@ class Request
public function path() public function path()
{ {
if (is_null($this->path)) { if (is_null($this->path)) {
$suffix = Config::get('url_html_suffix'); $suffix = Config::get('url_html_suffix');
$pathinfo = $this->pathinfo(); $pathinfo = $this->pathinfo();
if(false === $suffix){ if (false === $suffix) {
// 禁止伪静态访问 // 禁止伪静态访问
$this->path = $pathinfo; $this->path = $pathinfo;
}elseif($suffix){ } elseif ($suffix) {
// 去除正常的URL后缀 // 去除正常的URL后缀
$this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i' , '', $pathinfo); $this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
}else{ } else {
// 允许任何后缀访问 // 允许任何后缀访问
$this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo); $this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
} }
@@ -588,12 +588,12 @@ class Request
* @access public * @access public
* @param string|array $name 变量名 * @param string|array $name 变量名
* @param mixed $default 默认值 * @param mixed $default 默认值
* @param string|array $filter 过滤方法 * @param string|array $filter 过滤方法
* @return mixed * @return mixed
*/ */
public function param($name = '', $default = null, $filter = null) public function param($name = '', $default = null, $filter = null)
{ {
if(is_array($name)){ if (is_array($name)) {
// 设置param // 设置param
$this->param = array_merge($this->param, $name); $this->param = array_merge($this->param, $name);
return; return;
@@ -625,7 +625,7 @@ class Request
* @access public * @access public
* @param string|array $name 变量名 * @param string|array $name 变量名
* @param mixed $default 默认值 * @param mixed $default 默认值
* @param string|array $filter 过滤方法 * @param string|array $filter 过滤方法
* @return mixed * @return mixed
*/ */
public function get($name = '', $default = null, $filter = null) public function get($name = '', $default = null, $filter = null)
@@ -637,13 +637,13 @@ class Request
} }
return $this->input($this->get, $name, $default, $filter); return $this->input($this->get, $name, $default, $filter);
} }
/** /**
* 设置获取获取POST参数 * 设置获取获取POST参数
* @access public * @access public
* @param string $name 变量名 * @param string $name 变量名
* @param mixed $default 默认值 * @param mixed $default 默认值
* @param string|array $filter 过滤方法 * @param string|array $filter 过滤方法
* @return mixed * @return mixed
*/ */
public function post($name = '', $default = null, $filter = null) public function post($name = '', $default = null, $filter = null)
@@ -661,14 +661,14 @@ class Request
* @access public * @access public
* @param string|array $name 变量名 * @param string|array $name 变量名
* @param mixed $default 默认值 * @param mixed $default 默认值
* @param string|array $filter 过滤方法 * @param string|array $filter 过滤方法
* @return mixed * @return mixed
*/ */
public function put($name = '', $default = null, $filter = null) public function put($name = '', $default = null, $filter = null)
{ {
if (is_array($name)) { if (is_array($name)) {
return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name); return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name);
} }
if (is_null($this->put)) { if (is_null($this->put)) {
parse_str(file_get_contents('php://input'), $this->put); parse_str(file_get_contents('php://input'), $this->put);
} }
@@ -680,7 +680,7 @@ class Request
* @access public * @access public
* @param string|array $name 变量名 * @param string|array $name 变量名
* @param mixed $default 默认值 * @param mixed $default 默认值
* @param string|array $filter 过滤方法 * @param string|array $filter 过滤方法
* @return mixed * @return mixed
*/ */
public function delete($name = '', $default = null, $filter = null) public function delete($name = '', $default = null, $filter = null)
@@ -690,7 +690,7 @@ class Request
} }
if (is_null($this->delete)) { if (is_null($this->delete)) {
parse_str(file_get_contents('php://input'), $this->delete); parse_str(file_get_contents('php://input'), $this->delete);
} }
return $this->input($this->delete, $name, $default, $filter); return $this->input($this->delete, $name, $default, $filter);
} }
@@ -804,25 +804,25 @@ class Request
// 获取全部文件 // 获取全部文件
$item = []; $item = [];
foreach ($array as $key => $val) { foreach ($array as $key => $val) {
if($val instanceof File){ if ($val instanceof File) {
$item[$key] = $val; $item[$key] = $val;
}else{ } else {
if (empty($val['tmp_name'])) { if (empty($val['tmp_name'])) {
continue; continue;
} }
$item[$key] = new File($val['tmp_name'])->setUploadInfo($val); $item[$key] = (new File($val['tmp_name']))->setUploadInfo($val);
} }
} }
return $item; return $item;
} elseif (isset($array[$name])) { } elseif (isset($array[$name])) {
if($array[$name] instanceof File){ if ($array[$name] instanceof File) {
return $array[$name]; return $array[$name];
} elseif (!empty($array[$name]['tmp_name'])){ } elseif (!empty($array[$name]['tmp_name'])) {
return new File($array[$name]['tmp_name'])->setUploadInfo($array[$name]); return (new File($array[$name]['tmp_name']))->setUploadInfo($array[$name]);
} }
} }
} }
return null; return null;
} }
/** /**
@@ -838,7 +838,7 @@ class Request
return $this->env = array_merge($this->env, $name); return $this->env = array_merge($this->env, $name);
} elseif (empty($this->env)) { } elseif (empty($this->env)) {
$this->env = $_ENV; $this->env = $_ENV;
} }
return $this->input($this->env, strtoupper($name), $default, $filter); return $this->input($this->env, strtoupper($name), $default, $filter);
} }
@@ -854,12 +854,12 @@ class Request
if (is_array($name)) { if (is_array($name)) {
return $this->header = array_merge($this->header, $name); return $this->header = array_merge($this->header, $name);
} elseif (empty($this->header)) { } elseif (empty($this->header)) {
$header = []; $header = [];
$server = $this->server ?: $_SERVER; $server = $this->server ?: $_SERVER;
foreach($server as $key => $val) { foreach ($server as $key => $val) {
if (0 === strpos($key,'HTTP_')) { if (0 === strpos($key, 'HTTP_')) {
$key = str_replace('_','-',strtolower(substr($key,5))); $key = str_replace('_', '-', strtolower(substr($key, 5)));
$header[$key] = $val; $header[$key] = $val;
} }
} }
if (isset($server['CONTENT_TYPE'])) { if (isset($server['CONTENT_TYPE'])) {
@@ -867,14 +867,14 @@ class Request
} }
if (isset($server['CONTENT_LENGTH'])) { if (isset($server['CONTENT_LENGTH'])) {
$header['content-length'] = $server['CONTENT_LENGTH']; $header['content-length'] = $server['CONTENT_LENGTH'];
} }
$this->header = array_change_key_case($header); $this->header = array_change_key_case($header);
} }
if ('' === $name) { if ('' === $name) {
return $this->header; return $this->header;
} }
$name = str_replace('_','-',strtolower($name)); $name = str_replace('_', '-', strtolower($name));
return isset($this->header[$name]) ? $this->header[$name] : $default; return isset($this->header[$name]) ? $this->header[$name] : $default;
} }
/** /**
@@ -931,7 +931,7 @@ class Request
if (is_string($filter)) { if (is_string($filter)) {
$filter = explode(',', $filter); $filter = explode(',', $filter);
} else { } else {
$filter = (array)$filter; $filter = (array) $filter;
} }
$filter[] = $default; $filter[] = $default;
if (is_array($data)) { if (is_array($data)) {
@@ -976,7 +976,7 @@ class Request
// 调用函数或者方法过滤 // 调用函数或者方法过滤
$value = call_user_func($filter, $value); $value = call_user_func($filter, $value);
} else { } else {
if (strpos($filter,'/')) { if (strpos($filter, '/')) {
// 正则过滤 // 正则过滤
if (!preg_match($filter, $value)) { if (!preg_match($filter, $value)) {
// 匹配不成功返回默认值 // 匹配不成功返回默认值
@@ -1008,7 +1008,7 @@ class Request
if (is_string($value) && preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i', $value)) { if (is_string($value) && preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i', $value)) {
$value .= ' '; $value .= ' ';
} }
// TODO 其他安全过滤 // TODO 其他安全过滤
} }
/** /**
@@ -1052,7 +1052,7 @@ class Request
* @access public * @access public
* @param string $name 变量名 * @param string $name 变量名
* @param string $type 变量类型 * @param string $type 变量类型
* @param bool $checkEmpty 是否检测空值 * @param bool $checkEmpty 是否检测空值
* @return mixed * @return mixed
*/ */
public function has($name, $type = 'param', $checkEmpty = false) public function has($name, $type = 'param', $checkEmpty = false)
@@ -1069,7 +1069,7 @@ class Request
} else { } else {
return false; return false;
} }
} }
return ($checkEmpty && '' === $param) ? false : true; return ($checkEmpty && '' === $param) ? false : true;
} }