Home  >  Article  >  Backend Development  >  tp5 method code for batch uploading images

tp5 method code for batch uploading images

不言
不言Original
2018-08-20 16:37:484249browse

The content of this article is about the method code of tp5 for uploading images. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

tp5 batch uploading of images is actually very simple. The core principle is to upload a single image through a foreach loop to achieve the purpose of batch uploading. The essence is to upload individual images one by one. When we were working on the download station project, we implemented system configuration functions. This function can upload multiple pictures at the same time, such as website logos, QR codes, etc.

The core processing code for batch upload is as follows:

public function conflist(){
 if(request()->isPost()){
 $data=input('post.');
 $dataFile=$_FILES;
 foreach ($dataFile as $k => $v) {
 if($v['name']!=""){
 $data[$k]=$this->upload($k);
 }
 }
 foreach ($data as $k => $v) {
 db('config')->where(array('ename'=>$k))->update(['value'=>$v]);
 }
 $this->success('修改配置成功!');
 return;
 }
 $confRes=db('config')->select();
 $this->assign([
 'confRes'=>$confRes,
 ]);
 return view();
 }

The above is the logic, the upload function used during the loop is as follows:

public function upload($imgName){
 // 获取表单上传文件 例如上传了001.jpg
 $file = request()->file($imgName);
 // 移动到框架应用根目录/public/uploads/ 目录下
 $info = $file->move(ROOT_PATH . 'public' . DS . 'static/index/uploads');
 if($info){
 // 成功上传后 获取上传信息
 // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
 return $info->getSaveName();
 }else{
 // 上传失败获取错误信息
 return '';
 }

Related recommendations:

The sorting implementation code of infinite columns in thinkphp

Introduction to the method of sending emails using PHPMailer (with code)

The above is the detailed content of tp5 method code for batch uploading images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn