Home > Backend Development > PHP Tutorial > tp5 method code for batch uploading images

tp5 method code for batch uploading images

不言
Release: 2023-04-03 20:38:01
Original
4349 people have browsed it

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();
 }
Copy after login

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 '';
 }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template