97 lines
2.4 KiB
PHP
97 lines
2.4 KiB
PHP
|
<?php
|
|||
|
namespace Westdc\Dataimport;
|
|||
|
|
|||
|
class Upload{
|
|||
|
|
|||
|
private $config;
|
|||
|
|
|||
|
function __construct($db = NULL)
|
|||
|
{
|
|||
|
$this->config = \Zend_Registry::get('config');
|
|||
|
}
|
|||
|
|
|||
|
//上传數據
|
|||
|
public function __invoke($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 = $this->gen_uuid().".".$ext;
|
|||
|
|
|||
|
if(!preg_match("/(\\/|\\\)$/",$this->config->dataimport->path))
|
|||
|
$save_path = $this->config->dataimport->path . DIRECTORY_SEPARATOR;
|
|||
|
else
|
|||
|
$save_path = $this->config->dataimport->path;
|
|||
|
|
|||
|
$new_filepath = $save_path.$filename;
|
|||
|
|
|||
|
if (move_uploaded_file($file['tmp_name'], $new_filepath) === false) {
|
|||
|
return array("error"=>"上传失败,请重试");
|
|||
|
}
|
|||
|
|
|||
|
return array(
|
|||
|
"success" => 1,
|
|||
|
"file"=>$new_filepath
|
|||
|
);
|
|||
|
|
|||
|
}catch(Exception $e)
|
|||
|
{
|
|||
|
return array("error"=>$e->getMessage());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//获取文件扩展名
|
|||
|
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,通过finfo扩展
|
|||
|
public function getFileMime($file_name)
|
|||
|
{
|
|||
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|||
|
$filetype = finfo_file($finfo, $file_name) ; //文件mime类型
|
|||
|
finfo_close($finfo);
|
|||
|
return $filetype;
|
|||
|
}
|
|||
|
|
|||
|
//文件名uuid
|
|||
|
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 )
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
}
|