php读取localstorage,LocalStorage.php

/**

*

* @author Zhang Di

*/

namespace yiiwiki\storage;

use yii\base\Exception;

use yii\base\InvalidConfigException;

/**

* 本地存储

*

* @package yiiwiki\storage

*/

class LocalStorage extends BaseStorage

{

/**

* @var 存储文件的根目录

*/

public $basePath;

public function init()

{

if (!isset($this->basePath)) {

throw new InvalidConfigException('请配置 LocalStorage::basePath .');

}

}

/**

* 将内容写入文件

*

* @param string $filename

* @param string $content

* @return mixed

*/

public function write($filename, $content)

{

$realPath = $this->getRealPath($filename);

$dir = dirname($realPath);

if ($this->dirExists($dir, true)) {

return file_put_contents($realPath, $content);

} else {

return false;

}

}

/**

* 上传文件

*

* @param string $srcFilename

* @param string $distFilename

* @return mixed

*/

public function upload($srcFilename, $distFilename)

{

if (file_exists($srcFilename)) {

$realPath = $this->getRealPath($distFilename);

$this->dirExists(dirname($realPath));

return copy($srcFilename, $realPath);

} else {

$this->errorCode = self::ERROR_NO_SOURCE_FILE;

$this->errorMessage = "\$srcFilename '$srcFilename' 不存在。";

return false;

}

}

/**

* 获取文件内容

*

* @param string $filename

* @return mixed

*/

public function get($filename)

{

if ($this->exists($filename)) {

$realPath = $this->getRealPath($filename);

return file_get_contents($realPath);

} else {

throw new Exception("$filename 不存在。");

}

}

/**

* 文件是否存在

*

* @param string $filename

* @return boolean

*/

public function exists($filename)

{

$realPath = $this->getRealPath($filename);

return file_exists($realPath);

}

/**

* 根据 $filename 获取文件真是路径

*

* @param string $path

* @return string

*/

protected function getRealPath($filename)

{

return $this->basePath . DIRECTORY_SEPARATOR . $filename;

}

/**

* 文件件是否存在

*

* @param string $dir

* @param boolean $createIfNotExists 文件夹不存在时是否创建,默认为 false 不创建

* @return boolean

*/

public function dirExists($dir, $createIfNotExists = false)

{

if (!file_exists($dir)) {

if ($createIfNotExists) {

return mkdir($dir, 0777, true);

} else {

return false;

}

} else {

return true;

}

}

/**

* @inheritdoc

*/

public function delete($filename)

{

if ($this->exists($filename)) {

return unlink($this->getRealPath($filename));

} else {

return true;

}

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值