


PHP batch upload pictures and put the picture names into the database_PHP tutorial
Jul 13, 2016 am 10:32 AM
I was working on a function like this a few days ago. There are more than 800 members generated by the system; upload pictures to these more than 800 system members; and then put the picture names into the database.
Step one:
The first step is definitely to upload the image to the corresponding image directory, directly using the upload class already in the framework:
<?php try { $upload=new Upload(); $upload->set_ext(array('zip')); $path='目录名'; if ( ! Io::mkdir($path)) // 创建目录 { throw new Exception("无法创建文件上传目录:$path"); } $upload->set_path($path); if(!$upload->is_allow_ext($_FILES['files']['name'])) { $this->show_message('必须zip格式数据', '0', NULL, TRUE); } $result=$upload->save($_FILES['files']); $archive = new Archive_Zip(); $archive->set_target($path)->decompress($result['file']); unlink($result['file']);//删除使用后的zip; $this->show_message('导入成功', '1', array(array('text'=>'返回导入页面','href'=>'***跳转的链接地址***')),TRUE); }catch(Exception $e){ $this->show_message('图片导入失败', '0', NULL, TRUE); } } ?>
Step 2:
After the image upload is completed, you should take out the names of all the system member images in the directory, then you have to use traversal. Think about it, it doesn’t have to be so troublesome, PHP comes with the function glob();
glob() function returns the file name or directory matching the specified pattern.
This function returns an array containing matching files/directories. Returns false if an error occurs.
<?php //获取目录所有文件并将结果保存到数组 foreach(glob("目录名/*") as $d){ $tmp=explode('.',$d); $k=end($tmp); //如果是文件,并且后缀名为jpg png的文件 if(is_file($d)&&in_array($k,array('jpg','png'))){ $files[]=str_replace('******目录名/','',$d); } } ?>
During the loop, because I only wanted to get the picture name, I replaced the previous directory with empty.
Step 3:
After all the images are listed, it’s time to insert the image file names into the database.
Just write a loop.
First, use SELECT. . . . . . . . Find out the system members, and then calculate the number of system members,
<?php //查出系统会员 $member= DB::query(Database::SELECT, " SELECT * FROM 会员表名 WHERE 是否为系统会员 =1; ") ->execute() ->as_array(); ?>
Then insert into the database in a loop:
<?php for($i=0;$i<count($files);$i++){ $data = array( '图片字段' => $files[$i], ); DB::update('会员表') ->set($data) ->where('系统会员ID', '=', $member[$i]['系统会员ID']) ->execute(); } ?>
OK. That's it.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to make ppt pictures appear one by one

How does Go language implement the addition, deletion, modification and query operations of the database?

How to insert picture layout hexagonal cluster into PPT

Detailed tutorial on establishing a database connection using MySQLi in PHP

How does Hibernate implement polymorphic mapping?

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos

The operation process of setting several pictures to automatically play in a PPT slideshow

Analysis of the basic principles of MySQL database management system
