当前位置:网站首页>Example code of PHP for uploading multiple pictures
Example code of PHP for uploading multiple pictures
2022-06-10 16:57:00 【1024 Q】
First of all, you need to know this function
move_uploaded_file();
And then there's our input box , And our html page
Here we are adding input Box we can also write an attribute multiple="multiple" In this way, we can ctrl Multiple pictures selected
<html><head><title> Multiple file upload forms </title></head><body><style> form{ margin: 20px; padding: 10px; } #picInput>input{ display: block; margin: 10px; }</style><form action="pic.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <div id="picInput"> To upload pictures :<input type="file" name='myfile[]'> </div> <input id="addBtn" type="button" onclick="addPic1()" value=" Continue to add pictures "><br/><br/> <input type="submit" value=" Upload files "></form><script> function addPic1(){ var addBtn = document.getElementById('addBtn'); var input = document.createElement("input"); input.type = 'file'; input.name = 'myfile[]'; var picInut = document.getElementById('picInput'); picInut.appendChild(input); if(picInut.children.length == 3){ addBtn.disabled = 'disabled'; } }</script></body></html>PHP The method of inserting pictures into the background is not the same as before .
<meta charset="UTF-8"><?php$dbhost = 'localhost:3306'; // mysql Server host address $dbuser = 'root'; // mysql user name $dbpass = 'root'; // mysql User name, password $conn = mysqli_connect($dbhost, $dbuser, $dbpass);if(! $conn ){ die(' The connection fails : ' . mysqli_error($conn));}// Set encoding , Prevent Chinese miscoding mysqli_query($conn , "set names utf8");mysqli_select_db( $conn, 'image' );$file = $_FILES['myfile']; // Get the transmitted data , In the form of an array $name = $file['name']; // Get the file name , In the form of an array $upload_path = "img/"; // The storage path of the uploaded file // The current position foreach ($name as $key=>$names){//foreach take $name That is, all of our pictures name Key value pair output of , /* * strtolower() Make content lowercase , * substr() Method to extract from a string start The specified number of characters from which the subscript begins . This is to get us name The last path of . The starting subscript of the substring to extract . It must be a number . If it's a negative number , Then this parameter declares the position from the end of the string . in other words ,-1 Refers to the last character in the string * * */ $type = strtolower(substr($names,strrpos($names,'.')+1));// Get the file type , And it's all lowercase $allow_type = array('jpg','jpeg','gif','png'); // Define the types of uploads allowed // Remove the illegal format of the picture if (!in_array($type,$allow_type)){// Check whether the picture suffix is correct unset($name[$key]);// function , Parameter is name Is the value of the image name value . }}$str = '';// An empty string foreach ($name as $key=>$item){// Time to get pictures ; $type = strtolower(substr($item,strrpos($item,'.')+1));// Get the file type , And it's all lowercase if (move_uploaded_file($file['tmp_name'][$key],$upload_path.time().$name[$key])){ $str .= ','.$upload_path.time().$name[$key];// Splice the picture to a current time }else{// echo ' error '; }}// Assign to id Insert picture address ( Although it's an insertion , But it's updating fields , Don't get confused )$uid = 1;// Format $str = substr($str,1);// Last value to $str Then insert the content into the database $sql = "INSERT INTO img ". "(name) ". "VALUES ". "('$str')";$retval = mysqli_query( $conn, $sql );if(! $retval ){ die(' Unable to insert data : ' . mysqli_error($conn));}echo " Data insertion successful ";mysqli_close($conn);?>After successful insertion, read , Not the same as before . We're going to break up the fields , And then output our pictures circularly .
<meta charset="UTF-8"><style> .image{ width: 150px; height: 150px; }</style><?php$dbhost = 'localhost:3306'; // mysql Server host address $dbuser = 'root'; // mysql user name $dbpass = 'root'; // mysql User name, password $conn = mysqli_connect($dbhost, $dbuser, $dbpass);if(! $conn ){ die(' The connection fails : ' . mysqli_error($conn));}// Set encoding , Prevent Chinese miscoding mysqli_query($conn , "set names utf8");mysqli_select_db( $conn, 'image' );$sql = 'SELECT name FROM img';// Query database content $retval = mysqli_query( $conn, $sql );if(! $retval ){ die(' Unable to read data : ' . mysqli_error($conn));}$picpath = '';// Declare an empty string while($row = mysqli_fetch_array($retval)){ $picpath = $row[0];// Loop under our first field there is print , You can see .// var_dump($picpath);exit;}$picpath = explode(',',$picpath);// Decomposition indicator , We will be one , Divided into an array . Divided into parts .// Below you can print // var_dump($picpath);exit;$acs='';// Declare an empty string for($i=0; $i<count($picpath); $i++){// take $picpath Content , That is, the path of each picture is read . $acs=$picpath[$i];// Assign the content to our variable null . String . echo "<img class='image' src='".$acs."'>";// stay img Run in . }mysqli_close($conn);?>This is about PHP This is the end of the article about the sample code to realize the function of uploading multiple pictures , More about PHP Please search the previous articles of the software development network or continue to browse the following related articles. I hope you can support the software development network in the future !
边栏推荐
- Institute of automation, Chinese Academy of Sciences: a review of the latest visual language pre training
- Hidden Markov model and its training (1)
- 看先进科技如何改变人类生活
- 智慧景区视频监控 5G智慧灯杆网关组网综合杆
- From web2 to Web3, ideology also needs to change
- Swift read userinfo of remote notification
- Shit, jialichuang's price is reduced again
- Check whether there is a QR code in the picture
- Ar smart contact lens market prospect trends and development planning proposal report 2022-2028
- PyTorch基础(一)-- Anaconda 和 PyTorch安装
猜你喜欢

MFC基础知识与课程设计思路

Postman parameterization

Rethinking atlas revolution for semantic image segmentation (deeplobv3) personal summary

Application of stop in screen program_ SAP LIUMENG_

New York financial regulators issue official guidelines for stable currency

Carry forward the past and forge ahead into the future, multiple residuals | densenet (II)

Fosun Group hekaiduo: grow together with Alibaba cloud and help business innovation

This paper introduces three feature selection methods in machine learning

Digital image processing: graying

消除业务代码中if....else的五种方式
随机推荐
迪赛智慧数——文字(文本墙):80后儿童时期风靡的25种玩具
Application of stop in screen program_ SAP LIUMENG_
Simple file upload
隐形马尔可夫模型及其训练(一)
互联网企业与芯片
Ar smart contact lens market prospect trends and development planning proposal report 2022-2028
打造隐私计算领先方案 冲量在线数据互联平台获得鲲鹏Validated认证
Detailed steps for installing redis image in docker (easy to understand, suitable for novices to get started quickly)
Desai wisdom number - words (text wall): 25 kinds of popular toys for the post-80s children
From web2 to Web3, ideology also needs to change
谁在使用我的服务器?在干什么?什么时候?
防洪排涝监控 防洪排涝自动监测遥测终端
Research Report on the application field of Chinese antirust oil market and the prospect planning of the 14th five year plan (2022-2028)
Do you know the five GoLand shortcuts to improve efficiency?
Pictographic dynamic graphic ideographic data
Analysis report on market demand trend and sales strategy of global and Chinese automatic marshmallow machines (2022-2028)
Devops software architecture evolution
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
顺应医改,积极布局——集采背景下的高值医用耗材发展洞察2022
webdypro layout控件不能用_SAP刘梦