当前位置:网站首页>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 !


原网站

版权声明
本文为[1024 Q]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101554538575.html