<?php
namespace framework\components\upload;
use framework\base\Component;
class Upload extends Component
{
protected $_baseDir;
protected $_accept;
protected $_maxSize;
protected $_nameType; protected $_deep;
protected $_mime = array(
'image/jpeg' => 'jpg',
'image/bmp' => 'bmp',
'image/x-icon' => 'ico',
'image/gif' => 'gif',
'image/png' => 'png',
'blog/x-tar' => 'tar',
'blog/vnd.ms-powerpoint' => 'ppt',
'blog/pdf' => 'pdf',
'blog/x-shockwave-flash' => 'swf',
'blog/x-zip-compressed' => 'zip',
'blog/gzip' => 'gzip'
);
protected function init()
{
$this->_baseDir = APP_ROOT . \getModule() . '/' . $this->getValueFromConf('baseDir','runtime/upload');
$this->_accept = $this->getValueFromConf('accept', []);
$this->_maxSize = $this->getValueFromConf('maxSize', 0);
$this->_nameType = $this->getValueFromConf('nameType', 'time');
if ($this->_nameType !== 'md5')
return true;
$this->_deep = $this->getValueFromConf('deep', 2);
if ($this->_deep > 5)
{
$this->_deep = 5;
}
}
protected function getSavePath($name, $ext)
{
if (!\file_exists($this->_baseDir))
{
$dirs = \explode('/', $this->_baseDir);
$path = '';
foreach ($dirs as $item) {
$path .= $item;
if (!\file_exists($path)) {
\mkdir($path, 0755);
}
$path .= '/';
}
}
switch ($this->_nameType)
{
case 'md5':
$name = \mt_rand() . $name;
$name = \md5($name . SYSTEM_WORK_ID . \microtime());
$length = \strlen($name);
$particle = \ceil($length / $this->_deep);
$currentPath = '';
for ($i=0; $i<$this->_deep;$i++)
{
$tmpDir = \substr($name, $i*$particle, $particle);
$currentPath .= $tmpDir . '/';
if (!\file_exists($this->_baseDir . '/' . $currentPath))
{
\mkdir($this->_baseDir . '/' . $currentPath, 0755);
}
}
return $this->_baseDir . '/' . $currentPath . '/' . $name . '.' . $ext;
break;
case 'time':
default:
$name = \mt_rand() . $name;
$name = \md5($name . SYSTEM_WORK_ID . \microtime());
$subPath = \date('Ymd');
if (!\file_exists($this->_baseDir . '/' . $subPath))
{
\mkdir($this->_baseDir . '/' . $subPath, 0755);
}
return $this->_baseDir . '/' . $subPath . '/' . $name . '.' . $ext;
break;
}
}
public function getFileExt($file)
{
$s = \strrchr($file, '.');
if ($s === false)
{
return false;
}
return \strtolower(\trim(\substr($s, 1)));
}
protected function moveUploadFile($tmpfile, $newfile)
{ if (\rename($tmpfile, $newfile) === false)
{
return false;
}
return \chmod($newfile, 0666);
}
public function save($name)
{
if (empty($_FILES[$name]))
{
return false;
}
$fileSize = \filesize($_FILES[$name]['tmp_name']);
if ($this->_maxSize > 0 && $fileSize > $this->_maxSize)
{
return false;
}
$ext = $this->getFileExt($_FILES[$name]['name']);
if (!$ext)
{
return false;
}
$mime = $_FILES[$name]['type'];
if (!(isset($this->_mime[$mime]) && \in_array($this->_mime[$mime], $this->_accept)))
{ return false;
}
$fileSavePath = $this->getSavePath($_FILES[$name]['name'], $ext);
if ($this->moveUploadFile($_FILES[$name]['tmp_name'], $fileSavePath))
{
return \str_replace(APP_ROOT, '', $fileSavePath);
}
else
{
return false;
}
}
public function saveAll()
{
$return = [];
if($_FILES)
{
foreach($_FILES as $k=>$f)
{
$return[] = $this->save($k);
}
}
return $return;
}
}