Table of Contents
Step one:
Step 3:
Home Backend Development PHP Tutorial PHP batch upload pictures and put the picture names into the database_PHP tutorial

PHP batch upload pictures and put the picture names into the database_PHP tutorial

Jul 13, 2016 am 10:32 AM
picture database

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

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)&amp;&amp;in_array($k,array('jpg','png'))){
						  $files[]=str_replace('******目录名/','',$d);
				   }   
				}
?>
Copy after login

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


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();
							

				}
?>
Copy after login


OK. That's it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/755770.htmlTechArticleI had to do such a function at work a few days ago. There are more than 800 system-generated members; for these eight More than a hundred system members upload pictures; then the picture names are put into the database. Step one: Chapter...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make ppt pictures appear one by one How to make ppt pictures appear one by one Mar 25, 2024 pm 04:00 PM

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 does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

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

How to insert picture layout hexagonal cluster into PPT How to insert picture layout hexagonal cluster into PPT Mar 26, 2024 pm 05:10 PM

How to insert picture layout hexagonal cluster into PPT

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

Detailed tutorial on establishing a database connection using MySQLi in PHP

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

How does Hibernate implement polymorphic mapping?

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

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 The operation process of setting several pictures to automatically play in a PPT slideshow Mar 26, 2024 pm 02:01 PM

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

Analysis of the basic principles of MySQL database management system Analysis of the basic principles of MySQL database management system Mar 25, 2024 pm 12:42 PM

Analysis of the basic principles of MySQL database management system

See all articles