今天把项目中上传功能封装成类,方便后面使用,简单的封装了一下,感觉还不怎么好,后面继续优化。
具体代码如下:
<?php
/**
* created by phpstorm.
* user: wady www.bcty365.com
* date: 2017/8/16
* time: 14:52
*/
namespace appthinkclass;
use symfonycomponenthttpfoundationfileuploadedfile;
class uploadclass
{
/**
* @var uploadedfile $file;
*/
protected $file;
/**
* 上传错误信息
* @var string
*/
private $error = ''; //上传错误信息
private $fullpath='';//绝对地址
private $config = array(
'maxsize' => 3*1024*1024, //上传的文件大小限制 (0-不做限制)
'exts' => array('jpg','jpeg','gif','png','doc','docx','xls','xlsx','ppt','pptx','pdf','rar','zip'), //允许上传的文件后缀
'subname' => '', //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
'rootpath' => '/uploads/', //保存根路径
'savepath' => '', //保存路径
'thumb' => array(),//是裁剪压缩比例
);
public function __construct($config = array()){
/* 获取配置 */
$this->config = array_merge($this->config, $config);
if(!emptyempty($this->config['exts'])){
if (is_string($this->exts)){
$this->config['exts'] = explode(',', $this->exts);
}
$this->config['exts'] = array_map('strtolower', $this->exts);
}
$this->config['subname'] = $this->subname ? ltrim($this->subname,'/') : '/'.date('ymd');
$this->fullpath = rtrim(public_path(),'/').$this->config['rootpath'];
}
public function __get($name) {
return $this->config[$name];
}
public function __set($name,$value){
if(isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
public function __isset($name){
return isset($this->config[$name]);
}
/**
* 获取最后一次上传错误信息
* @return string 错误信息
*/
public function geterror(){
return $this->error;
}
public function upload($file){
if(emptyempty($file)){
$this->error = '没有上传的文件';
return false;
}
if(!$this->checkrootpath($this->fullpath)){
$this->error = $this->geterror();
return false;
}
$filesavepath=$this->fullpath.$this->savepath.$this->subname;
if(!$this->checksavepath($filesavepath)){
$this->error = $this->geterror();
return false;
}
$files =array();
if(!is_array($file)){
//如果不是数组转成数组
$files[]=$file;
}else{
$files=$file;
}
$info = array();
$imgthumb = new appthinkclassthumbclass();
foreach ($files as $key=>$f){
$this->file=$f;
$f->ext = strtolower($f->getclientoriginalextension());
/*文件上传检查*/
if (!$this->check($f)){
continue;
}
$filename = str_random(12).'.'.$f->ext;
/* 保存文件 并记录保存成功的文件 */
if ($this->file->move($filesavepath,$filename)) {
/*图片按照宽高比例压缩*/
log::notice($filesavepath.$filename);
if(!emptyempty($this->thumb) && is_array($this->thumb)){
$imgthumb ->thumb($this->thumb,$filesavepath.'/'.$filename);
}
$info[]=$this->rootpath.$this->savepath.$this->subname.'/'.$filename;
}
}
return is_array($info) ? $info : false;
}
/**
* 检测上传根目录
* @param string $rootpath 根目录
* @return boolean true-检测通过,false-检测失败
*/
protected function checkrootpath($rootpath){
if(!(is_dir($rootpath) && is_writable($rootpath))){
$this->error = '上传根目录不存在!';
return false;
}
return true;
}
/**
* 检测上传目录
* @param string $savepath 上传目录
* @return boolean 检测结果,true-通过,false-失败
*/
public function checksavepath($savepath){
/* 检测并创建目录 */
if (!$this->mkdir($savepath )) {
return false;
} else {
/* 检测目录是否可写 */
if (!is_writable($savepath)) {
$this->error = '上传目录不可写!';
return false;
} else {
return true;
}
}
}
/**
* 检查上传的文件
* @param array $file 文件信息
*/
private function check($file) {
/* 检查文件大小 */
if (!$this->checksize($file->getsize())) {
$this->error = '上传文件大小不符!';
return false;
}
/* 检查文件后缀 */
if (!$this->checkext($file->ext)) {
$this->error = '上传文件后缀不允许';
return false;
}
/* 通过检测 */
return true;
}
/**
* 检查文件大小是否合法
* @param integer $size 数据
*/
private function checksize($size) {
return !($size > $this->maxsize) || (0 == $this->maxsize);
}
/**
* 检查上传的文件后缀是否合法
* @param string $ext 后缀
*/
private function checkext($ext) {
return emptyempty($this->config['exts']) ? true : in_array(strtolower($ext), $this->exts);
}
/**
* 创建目录
* @param string $savepath 要创建的穆里
* @return boolean 创建状态,true-成功,false-失败
*/
protected function mkdir($savepath){
if(is_dir($savepath)){
return true;
}
if(mkdir($savepath, 0777, true)){
return true;
} else {
$this->error = "目录创建失败";
return false;
}
}
}
使用案例:
头部引用 use appthinkclassuploadclass;
$upload = new uploadclass();
$upload->exts=array('jpg','png');
$upload->maxsize=5*1024*1024;
$upload->savepath='course/uid_6';
$file = $request->file('fileimg');
$aa = $upload->upload($file);
dd($aa);
总结
以上所述是小编给大家介绍的php laravel 上传图片、文件等类封装,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对硕编程网站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!