You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.4 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Think\Upload\Driver;
class Local{
/**
* 上传文件根目录
* @var string
*/
private $rootPath;
/**
* 本地上传错误信息
* @var string
*/
private $error = ''; //上传错误信息
/**
* 构造函数,用于设置上传根路径
*/
public function __construct($config = null){
}
/**
* 检测上传根目录
* @param string $rootpath 根目录
* @return boolean true-检测通过false-检测失败
*/
public function checkRootPath($rootpath){
if(!(is_dir($rootpath) && is_writable($rootpath))){
$this->error = '上传根目录不存在!请尝试手动创建:'.$rootpath;
return false;
}
$this->rootPath = $rootpath;
return true;
}
/**
* 检测上传目录
* @param string $savepath 上传目录
* @return boolean 检测结果true-通过false-失败
*/
public function checkSavePath($savepath){
/* 检测并创建目录 */
if (!$this->mkdir($savepath)) {
return false;
} else {
/* 检测目录是否可写 */
if (!is_writable($this->rootPath . $savepath)) {
$this->error = '上传目录 ' . $savepath . ' 不可写!';
return false;
} else {
return true;
}
}
}
/**
* 保存指定文件
* @param array $file 保存的文件信息
* @param boolean $replace 同名文件是否覆盖
* @return boolean 保存状态true-成功false-失败
*/
public function save($file, $replace=true) {
$filename = $this->rootPath . $file['savepath'] . $file['savename'];
/* 不覆盖同名文件 */
if (!$replace && is_file($filename)) {
$this->error = '存在同名文件' . $file['savename'];
return false;
}
/* 移动文件 */
if (!move_uploaded_file($file['tmp_name'], $filename)) {
$this->error = '文件上传保存错误!';
return false;
}
return true;
}
/**
* 创建目录
* @param string $savepath 要创建的穆里
* @return boolean 创建状态true-成功false-失败
*/
public function mkdir($savepath){
$dir = $this->rootPath . $savepath;
if(is_dir($dir)){
return true;
}
if(mkdir($dir, 0777, true)){
return true;
} else {
$this->error = "目录 {$savepath} 创建失败!";
return false;
}
}
/**
* 获取最后一次上传错误信息
* @return string 错误信息
*/
public function getError(){
return $this->error;
}
}