179 lines
3.7 KiB
PHP
179 lines
3.7 KiB
PHP
|
<?php
|
||
|
namespace Westdc\Dataimport;
|
||
|
|
||
|
class File{
|
||
|
|
||
|
private $config;
|
||
|
|
||
|
function __construct($db = NULL)
|
||
|
{
|
||
|
$this->config = \Zend_Registry::get('config');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 獲取文件存儲目錄
|
||
|
* @return string
|
||
|
*/
|
||
|
private function getSavePath()
|
||
|
{
|
||
|
if(!preg_match("/(\\/|\\\)$/",$this->config->dataimport->path))
|
||
|
return $this->config->dataimport->path . DIRECTORY_SEPARATOR;
|
||
|
else
|
||
|
return $this->config->dataimport->path;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 上傳文件
|
||
|
* @param $file
|
||
|
* @return array
|
||
|
*/
|
||
|
public function upload($file)
|
||
|
{
|
||
|
try{
|
||
|
if (empty($file) !== false) {
|
||
|
return array("error"=>"请选择要上传的文件");
|
||
|
}
|
||
|
|
||
|
if (@is_uploaded_file($file['tmp_name']) === false) {
|
||
|
return array("error"=>"文件上传失败,请重新上传");
|
||
|
}
|
||
|
|
||
|
if($file['size'] > 20 * 1024 * 1024)
|
||
|
{
|
||
|
return array('error'=>"文件大小超出限制");
|
||
|
}
|
||
|
|
||
|
$ext = $this->getFileTextExt($file['name']);
|
||
|
$filename = $file['name']."_".$this->gen_uuid().".".$ext;
|
||
|
|
||
|
$new_filepath = $this->getSavePath().$filename;
|
||
|
|
||
|
if (move_uploaded_file($file['tmp_name'], $new_filepath) === false) {
|
||
|
return array("error"=>"上传失败,请重试");
|
||
|
}
|
||
|
|
||
|
return array(
|
||
|
"success" => 1,
|
||
|
"file"=>$filename
|
||
|
);
|
||
|
|
||
|
}catch(Exception $e)
|
||
|
{
|
||
|
return array("error"=>$e->getMessage());
|
||
|
}
|
||
|
}//upload()
|
||
|
|
||
|
/**
|
||
|
* 获取文件扩展名
|
||
|
* @param $file_name
|
||
|
* @return mixed|string
|
||
|
*/
|
||
|
public function getFileTextExt($file_name)
|
||
|
{
|
||
|
$temp_arr = explode(".", $file_name);
|
||
|
$file_ext = array_pop($temp_arr);
|
||
|
$file_ext = trim($file_ext);
|
||
|
$file_ext = strtolower($file_ext);
|
||
|
return $file_ext;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 獲取文件Mime
|
||
|
* @param $file_name
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function getFileMime($file_name)
|
||
|
{
|
||
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||
|
$filetype = finfo_file($finfo, $file_name) ; //文件mime类型
|
||
|
finfo_close($finfo);
|
||
|
return $filetype;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 生成文件名UUID
|
||
|
* @return string
|
||
|
*/
|
||
|
public function gen_uuid() {
|
||
|
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||
|
// 32 bits for "time_low"
|
||
|
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
|
||
|
|
||
|
// 16 bits for "time_mid"
|
||
|
mt_rand( 0, 0xffff ),
|
||
|
|
||
|
// 16 bits for "time_hi_and_version",
|
||
|
// four most significant bits holds version number 4
|
||
|
mt_rand( 0, 0x0fff ) | 0x4000,
|
||
|
|
||
|
// 16 bits, 8 bits for "clk_seq_hi_res",
|
||
|
// 8 bits for "clk_seq_low",
|
||
|
// two most significant bits holds zero and one for variant DCE1.1
|
||
|
mt_rand( 0, 0x3fff ) | 0x8000,
|
||
|
|
||
|
// 48 bits for "node"
|
||
|
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 獲得已經上傳的文件
|
||
|
* @return array
|
||
|
*/
|
||
|
public function getUploadFiles()
|
||
|
{
|
||
|
return $this->fetchDir($this->getSavePath());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 掃描文件夾
|
||
|
* @param $directory
|
||
|
* @return array
|
||
|
*/
|
||
|
public function fetchDir($directory){
|
||
|
$mydir = dir($directory);
|
||
|
|
||
|
$files = [];
|
||
|
|
||
|
while($file = $mydir->read())
|
||
|
{
|
||
|
if($file == '.' || $file == "..")
|
||
|
continue;
|
||
|
|
||
|
if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!=".."))
|
||
|
{
|
||
|
$files = array_merge($files,$this->fetchDir("$directory/$file"));
|
||
|
}
|
||
|
else
|
||
|
$files[] = $file;
|
||
|
}
|
||
|
|
||
|
$mydir->close();
|
||
|
|
||
|
return $files;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 刪除文件
|
||
|
* @param $file
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function deleteFile($file)
|
||
|
{
|
||
|
if(empty($file))
|
||
|
return false;
|
||
|
|
||
|
return unlink($this->getSavePath() . $file);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 獲取文件真實路徑
|
||
|
* @param $file
|
||
|
* @return string
|
||
|
*/
|
||
|
public function getRealName($file)
|
||
|
{
|
||
|
return $this->getSavePath().$file;
|
||
|
}
|
||
|
|
||
|
}
|