class upload
{
public $_file;
public function __construct( $name =null)
{
if(is_null($name) || !isset($_FILES[$name]))
$name = key($_FILES);
if(!isset($_FILES[$name]))
throw new Exception("并没有文件上传");
$this->_file = $_FILES[$name];
if(!is_uploaded_file($this->_file['tmp_name']))
throw new Exception("异常情况");
if($this->_file['error'] !== 0)
throw new Exception("错误代码:".$this->_file['error']);
}
public function moveTo( $new_dir)
{
$real_dir = $this->checkDir($new_dir);
return move_uploaded_file($this->_file['tmp_name'], $real_dir.'/'.$this->_file['name']);
}
private function checkDir($dir)
{
$real_dir = realpath($dir);
if($real_dir === false)
throw new Exception("给定目录{$dir}不存在");
if(!is_writable($real_dir))
throw new Exception("给定目录{$dir}不可写");
return $real_dir;
}
}
|