Local.class.php.bak 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Upload\Driver;
  12. class Local{
  13. /**
  14. * 上传文件根目录
  15. * @var string
  16. */
  17. private $rootPath;
  18. /**
  19. * 本地上传错误信息
  20. * @var string
  21. */
  22. private $error = ''; //上传错误信息
  23. /**
  24. * 构造函数,用于设置上传根路径
  25. */
  26. public function __construct($config = null){
  27. }
  28. /**
  29. * 检测上传根目录
  30. * @param string $rootpath 根目录
  31. * @return boolean true-检测通过,false-检测失败
  32. */
  33. public function checkRootPath($rootpath){
  34. if(!(is_dir($rootpath) && is_writable($rootpath))){
  35. $rootpath=APP_REALPATH.$rootpath;
  36. if(!(is_dir($rootpath) && is_writable($rootpath))){
  37. $this->error = '上传根目录不存在!请尝试手动创建:'.$rootpath;
  38. return false;
  39. }
  40. }
  41. $this->rootPath = $rootpath;
  42. return true;
  43. }
  44. /**
  45. * 检测上传目录
  46. * @param string $savepath 上传目录
  47. * @return boolean 检测结果,true-通过,false-失败
  48. */
  49. public function checkSavePath($savepath){
  50. /* 检测并创建目录 */
  51. if (!$this->mkdir($savepath)) {
  52. return false;
  53. } else {
  54. /* 检测目录是否可写 */
  55. if (!is_writable($this->rootPath . $savepath)) {
  56. $this->error = '上传目录 ' . $savepath . ' 不可写!';
  57. return false;
  58. } else {
  59. return true;
  60. }
  61. }
  62. }
  63. /**
  64. * 保存指定文件
  65. * @param array $file 保存的文件信息
  66. * @param boolean $replace 同名文件是否覆盖
  67. * @return boolean 保存状态,true-成功,false-失败
  68. */
  69. public function save($file, $replace=true) {
  70. $filename = $this->rootPath . $file['savepath'] . $file['savename'];
  71. /* 不覆盖同名文件 */
  72. if (!$replace && is_file($filename)) {
  73. $this->error = '存在同名文件' . $file['savename'];
  74. return false;
  75. }
  76. /* 移动文件 */
  77. if (!move_uploaded_file($file['tmp_name'], $filename)) {
  78. //if (!move_uploaded_file(realpath($file['tmp_name']), $filename)) {
  79. $this->error = '文件上传保存错误!';
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * 创建目录
  86. * @param string $savepath 要创建的穆里
  87. * @return boolean 创建状态,true-成功,false-失败
  88. */
  89. public function mkdir($savepath){
  90. $dir = $this->rootPath . $savepath;
  91. if(is_dir($dir)){
  92. return true;
  93. }
  94. if(mkdir($dir, 0777, true)){
  95. return true;
  96. } else {
  97. $this->error = "目录 {$savepath} 创建失败!";
  98. return false;
  99. }
  100. }
  101. /**
  102. * 获取最后一次上传错误信息
  103. * @return string 错误信息
  104. */
  105. public function getError(){
  106. return $this->error;
  107. }
  108. }