问题描述
<$ c $c>< form action =upload.phpmethod =postenctype =multipart / form-data>
< p> Image1:< input name =image1type =file/>< / p>
< p> Image2:< input name =image2type =file/>< / p>
< p> Image3:< input name =image3type =file/>< / p>
< input type =submitvalue =Submit/>
< / form>
include('configdb.php');
$ uploadDir ='upload /';
if(isset($ _ POST ['submit']))
{
$ fileName = $ _FILES ['image1'] ['name'];
$ tmpName = $ _FILES ['image1'] ['tmp_name'];
$ fileSize = $ _FILES ['image1'] ['size'];
$ fileType = $ _FILES ['image1'] ['type'];
$ image1path = $ uploadDir。 $文件名;
$ result = move_uploaded_file($ tmpName,$ image1path);
if(!$ result){
echoError uploadloading;
出口; ($!
if(!get_magic_quotes_gpc())
{
$ fileName = addslashes($ fileName);
$ image1 = addslashes($ image1path);
$ b $ sql =插入图片(image1,image2,image3)VALUES('$ image1','$ image2',$ image3) ;
$ b $ if(!mysql_query($ sql,$ con))
{
die('Error:'。mysql_error());
$ b $ / code $ / pre
基本上我的代码是一个图像上传,我怎样才能让一个函数能够上传3张图片。解决方案这里是多个上传图片的代码。您可以上传超过3张图片。
< form action =upload.phpmethod =postenctype =多部分/格式数据>
< p> Image1:< input name =image []type =file/>< / p>
< p> Image2:< input name =image []type =file/>< / p>
< p> Image3:< input name =image []type =file/>< / p>
< input type =submitname =submitvalue =Submit/>
< / form>
include('configdb.php');
$ uploadDir ='upload /';
if(isset($ _ POST ['submit'])){
$ image = array();
$ b foreach($ _ FILES ['image'] ['name'] as $ index => $ name){
if($ _ FILES ['image'] [ 'error'] [$ index] == 4){
continue;
$ b $ if($ _ FILES ['image'] ['error'] [$ index] == 0){
$ fileName = $ _FILES [ '图像'] [ '名称'] [$指数];
$ tmpName = $ _FILES ['image'] ['tmp_name'] [$ index];
$ fileSize = $ _FILES ['image'] ['size'] [$ index];
$ fileType = $ _FILES ['image'] ['type'] [$ index];如果($ fileType ==image / gif||
$ fileType ==image / jpeg||
$ fileType ==image / pjpeg)
||
$ fileType ==image / png||
$ fileType ==image / x-png)&&
$ fileSize< 500000){
$ imagePath = $ uploadDir。 $文件名;
$ result = @move_uploaded_file($ tmpName,$ imagePath);
if(!$ result){
echoError uploadloading;
出口;
}
$ image [] = $ imagePath;
$ b //将图像保存到数据库
$ nbImage = count($ image);
if($ nbImage){
$ sql =INSERT INTO picture(image1,image2,image3)VALUES(;
for($ i = 0; $ i< $ nbImage; $ i ++){
if($ i)$ sql。=,;
$ sql。=\。$ image [$ i]。\ ;
}
$ sql。=);
@mysql_query($ sql);
}
}
?>
注意:由于安全性的原因,您应该在上传之前测试图片的类型和大小。 / p>
Hi there I want to make a function for me to able upload a multiple image in one submission below are my code structure:
<form action="upload.php" method="post" enctype="multipart/form-data"> <p>Image1 :<input name="image1" type="file" /></p> <p>Image2 :<input name="image2" type="file" /></p> <p>Image3 :<input name="image3" type="file" /></p> <input type="submit" value="Submit" /> </form> include('configdb.php'); $uploadDir = 'upload/'; if(isset($_POST['submit'])) { $fileName = $_FILES['image1']['name']; $tmpName = $_FILES['image1']['tmp_name']; $fileSize = $_FILES['image1']['size']; $fileType = $_FILES['image1']['type']; $image1path = $uploadDir . $fileName; $result = move_uploaded_file($tmpName, "$image1path"); if (!$result) { echo "Error uploading"; exit; } if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $image1 = addslashes($image1path); } $sql="INSERT INTO picture (image1, image2, image3) VALUES ('$image1','$image2', $image3)"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } }
Basically my code above does one image upload, How can I make a function to be able upload 3 images.
解决方案Here is the code to multiple upload image. You can upload more than 3 images.
<form action="upload.php" method="post" enctype="multipart/form-data"> <p>Image1 :<input name="image[]" type="file" /></p> <p>Image2 :<input name="image[]" type="file" /></p> <p>Image3 :<input name="image[]" type="file" /></p> <input type="submit" name="submit" value="Submit" /> </form> <?php include('configdb.php'); $uploadDir = 'upload/'; if(isset($_POST['submit'])) { $image = array(); foreach($_FILES['image']['name'] as $index => $name) { if($_FILES['image']['error'][$index] == 4) { continue; } if($_FILES['image']['error'][$index] == 0) { $fileName = $_FILES['image']['name'][$index]; $tmpName = $_FILES['image']['tmp_name'][$index]; $fileSize = $_FILES['image']['size'][$index]; $fileType = $_FILES['image']['type'][$index]; if(($fileType == "image/gif" || $fileType == "image/jpeg" || $fileType == "image/pjpeg" || $fileType == "image/png" || $fileType == "image/x-png") && $fileSize < 500000) { $imagePath = $uploadDir . $fileName; $result = @move_uploaded_file($tmpName, $imagePath); if (!$result) { echo "Error uploading"; exit; } $image[] = $imagePath; } } } // Save images to database $nbImage = count($image); if($nbImage) { $sql = "INSERT INTO picture (image1, image2, image3) VALUES ("; for($i=0; $i<$nbImage; $i++) { if($i) $sql .= ","; $sql .= "\"".$image[$i]."\""; } $sql .= ")"; @mysql_query($sql); } } ?>
Note: you should test the type and the size of image before upload it because of the security.
这篇关于多重上传图像功能的PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!