<?php //cfgupload.php 配置文件
return array (
'UPLOAD' =>
array (
'maxSize' => 2097152,
'exts' =>
array (
0 => 'jpg',
1 => 'gif',
2 => 'png',
3 => 'jpeg',
4=>"mp3",
),
'rootPath' => '/mnt/img.imwinlion.com/',
'savePath' => '',
'driver' => 'Local',
'subType' => 'hash',
'hashLevel' => 1,
'domain' => 'res1.imwinlion.com',
'scheme' => 'http',
'subDir' => 'upload',
),
);
/*该函数处理前端上传过来的base64场景*/
function uploadb64(){
$uploadcfg=C('UPLOAD');
$hashLevel = intval($uploadcfg["hashLevel"]);
$rootPath = $uploadcfg["rootPath"];
$savePath = $uploadcfg["savePath"];
$subDir = $uploadcfg["subDir"];
$scheme = $uploadcfg["scheme"];
$domain = $uploadcfg["domain"];
$dir = "";
$filename = md5(microtime()).".jpg";
$str = "0123456789abcdef";
$a1 = "";
$a2 = "";
if($hashLevel!=2 && $hashLevel!=1){
$$hashLevel = 1;
}
if($hashLevel==1){
$a1 = $str[mt_rand(0,strlen($str))];
$savePath = $a1;
$filename = $hashLevel. $a1 .$a2. $filename;
}
if($hashLevel==2){
$a1 = $str[mt_rand(0,strlen($str))];
$a2 = $str[mt_rand(0,strlen($str))];
$savePath = $a1 ."/" .$a2 ;
$filename = $hashLevel . $a1 .$a2. $filename;
}
//根据策略获得hostid
$filename .= ($this->gethostid(0) . $filename);
$this->base64toimage($this->request["data"],$rootPath . "/" . $subDir ."/". $savePath ."/". $filename );
$resp = array();
$url = $scheme. "://" . $domain . "/". $subDir ."/".$savePath ."/". $filename ;
$resp["url"] = $url;
$resp["error"] = 0;
$resp["state"]="SUCCESS";
if($this->request["callback"]) {
echo '<script>'.$callback.'('.json_encode($resp).')</script>';
} else {
echo json_encode($resp);
}
}
function base64toimage($strBase64, $outputFile ) {
$ifp = fopen( $outputFile, "wb" );
fwrite( $ifp, base64_decode( $strBase64) );
fclose( $ifp );
return( $outputFile );
}
/*这里有个技巧,我们根据入参业务频繁程度获取if*/
function gethostid($type=0){
//存那一台服务器逻辑需要我们自己编写
//我们这里演示 默认定死了 为1
return 1;
}
ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。