<?php
namespace app\common\library\storage\engine;
use app\common\library\helper;
class Local extends Server
{
private $uploadPat='';
public function __construct()
{
parent::__construct();
}
public function upload()
{
return $this->isInternal ? $this->uploadByInternal() : $this->uploadByExternal();
}
private function uploadByExternal()
{
$uplodDir = WEB_PATH . 'uploads';
$info = $this->file->validate([
'size' => 4 * 1024 * 1024,
'ext' => 'mp4,mov,jpg,jpeg,png,gif'
]);
$ext = $this->getFileInfo();
$ex_type =explode('/', $ext['type']);
$type = $ex_type[0];
if($type=='video'){
$uplodDir.='/video/'.date("Y",time()).'/'.date('m',time()).'/'.date("d",time());
}
if($type=='image'){
$uplodDir.='/image/'.date("Y",time()).'/'.date('m',time()).'/'.date("d",time());
}
$this->uploadPat = $uplodDir;
if (!file_exists($uplodDir)){
mkdir($uplodDir);
}
$info->move($uplodDir, $this->fileName);
if (empty($info)) {
$this->error = $this->file->getError();
return false;
}
return true;
}
private function uploadByInternal()
{
$uplodDir = WEB_PATH . 'uploads';
$realPath = $this->getRealPath();
$ext = $this->getFileInfo();
$ex_type =explode('/', $ext['type']);
$type = $ex_type[0];
if($type=='video'){
$uplodDir.='/video/'.date("Y",time()).'/'.date('m',time()).'/'.date("d",time());
}
if($type=='image'){
$uplodDir.='/image/'.date("Y",time()).'/'.date('m',time()).'/'.date("d",time());
}
if (!file_exists($uplodDir)){
mkdir($uplodDir);
}
$this->uploadPat = $uplodDir;
if (!rename($realPath, "{$uplodDir}/$this->fileName")) {
$this->error = 'upload write error';
return false;
}
return true;
}
public function delete($fileName)
{
$filePath = WEB_PATH . "uploads/{$fileName}";
return !file_exists($filePath) ?: unlink($filePath);
}
public function getFileName()
{
$ext = $this->getFileInfo();
$ex_type =explode('/', $ext['type']);
$type = $ex_type[0];
if($type=='video'){
$uplodDir='video/'.date("Y",time()).'/'.date('m',time()).'/'.date("d",time());
}
if($type=='image'){
$uplodDir='image/'.date("Y",time()).'/'.date('m',time()).'/'.date("d",time());
}
return $uplodDir.'/'.$this->fileName;
}
}