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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:32:29973browse

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.

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:
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