search
Homephp教程php手册php批量上传图片并把图片名放入数据库

前几天工作中要做这样一个功能,有八百多个系统 生成的会员;给这八百多个系统会员上传图片;然后把图片名放入数据库。

第一步:

第一步肯定是首先把图片上传到对应的图片目录下,直接用框架中已经有的上传类:

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

第二步:

图片上传完成后,就应该把目录下所有的系统会员图片名称取出来,那就得用遍历了。想想也不用那么麻烦了,php自带函数glob();

glob() 函数返回匹配指定模式的文件名或目录。

该函数返回一个包含有匹配文件 / 目录的数组。如果出错返回 false。


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

循环过程中因为我只想得到图片名称,所以我把前面的目录替换为空。



第三步:

列出所有的图片后就该把图片文件名插入数据库了。

写个循环就可以了啊。

首先,用SELECT。。。。。。。。查出系统会员,然后计算出系统会员的个数,

<?php 
			 //查出系统会员
			$member= DB::query(Database::SELECT,
			" SELECT * FROM 会员表名 WHERE 是否为系统会员 =1; ")  
			->execute()
			->as_array();
?>


然后再循环插入数据库:

<?php
				for($i=0;$i<count($files);$i++){
								$data = array(
									'图片字段' => $files[$i],
								);
								DB::update('会员表')
									->set($data)
									->where('系统会员ID', '=', $member[$i]['系统会员ID'])
									->execute();
							

				}
?>


OK。就这样就搞定了。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool