Local.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * 上传文件根目录
  16. * @var string
  17. */
  18. private $rootPath;
  19. /**
  20. * 本地上传错误信息
  21. * @var string
  22. */
  23. private $error = ''; //上传错误信息
  24. /**
  25. * 构造函数,用于设置上传根路径
  26. */
  27. public function __construct($config = null)
  28. {
  29. }
  30. /**
  31. * 检测上传根目录
  32. * @param string $rootpath 根目录
  33. * @return boolean true-检测通过,false-检测失败
  34. */
  35. public function checkRootPath($rootpath)
  36. {
  37. if (!(is_dir($rootpath) && is_writable($rootpath))) {
  38. $this->error = '上传根目录不存在!请尝试手动创建:' . $rootpath;
  39. return false;
  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. /* 检测并创建目录 */
  52. if (!$this->mkdir($savepath)) {
  53. return false;
  54. } else {
  55. /* 检测目录是否可写 */
  56. if (!is_writable($this->rootPath . $savepath)) {
  57. $this->error = '上传目录 ' . $savepath . ' 不可写!';
  58. return false;
  59. } else {
  60. return true;
  61. }
  62. }
  63. }
  64. /**
  65. * 保存指定文件
  66. * @param array $file 保存的文件信息
  67. * @param boolean $replace 同名文件是否覆盖
  68. * @return boolean 保存状态,true-成功,false-失败
  69. */
  70. public function save($file, $replace = true)
  71. {
  72. $filename = $this->rootPath . $file['savepath'] . $file['savename'];
  73. /* 不覆盖同名文件 */
  74. if (!$replace && is_file($filename)) {
  75. $this->error = '存在同名文件' . $file['savename'];
  76. return false;
  77. }
  78. /* 移动文件 */
  79. if (!move_uploaded_file($file['tmp_name'], $filename)) {
  80. $this->error = '文件上传保存错误!';
  81. return false;
  82. }
  83. return true;
  84. }
  85. /**
  86. * 创建目录
  87. * @param string $savepath 要创建的目录
  88. * @return boolean 创建状态,true-成功,false-失败
  89. */
  90. public function mkdir($savepath)
  91. {
  92. $dir = $this->rootPath . $savepath;
  93. if (is_dir($dir)) {
  94. return true;
  95. }
  96. if (mkdir($dir, 0777, true)) {
  97. return true;
  98. } else {
  99. $this->error = "目录 {$savepath} 创建失败!";
  100. return false;
  101. }
  102. }
  103. /**
  104. * 获取最后一次上传错误信息
  105. * @return string 错误信息
  106. */
  107. public function getError()
  108. {
  109. return $this->error;
  110. }
  111. }