type是根据不同的文件类型,在方法中可以进行不同的处理
/**
* [上传音频文件到服务器]
* @param [object] $file [文件资源]
* @param [string] $type [图片类型]
* @return [string] [图片在服务器的路径]
*/
public function uploadFiles02($file, $type = '') {
$info = $file->move(ROOT_PATH . 'public' . DS . 'temp01');
if ($info) {
//获取图片路径
$path = '/temp01/' . $info->getSaveName();
$path = preg_replace('/\\\\/', '/', $path);
//裁剪图片
if (!empty($type)) {
$this->imageEdit($path, $type);
}
} else {
$this->returnMsg(400, $file->getError());
}
return $path;
}
调用上述方法
//1. 接收参数
$this->datas = $this->params;
//2. 上传文件获取路径
$head_img_path = $this->uploadFiles($this->datas['user_icon'], 'head_img');
除此之外,还有下面这个方式,当然很类似,只是根据不同的场景使用
/**
* [上传文件到服务器]
* @param [object] $file [文件资源]
* @param [string] $type [图片类型]
* @return [string] [图片在服务器的路径]
*/
public function uploadFiles($file, $type = '')
{
$name = $file->getInfo()['name'];
//dump($file);
//dump($name);die;
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads',$name);
if ($info) {
//获取图片路径
$path = '/uploads/' . $info->getSaveName();
$path = iconv("GB2312","UTF-8", $path);
$path = preg_replace('/\\\\/', '/', $path);
//裁剪图片
if (!empty($type)) {
$this->imageEdit($path, $type);
}
} else {
$this->returnMsg(400, $file->getError());
}
return $path;
}