多个图片上传

时间:2014-01-27 05:02:40

标签: php

我在多张图片上传时出现问题,当我上传时,只有1张图片会转到目标目录和数据库。我需要两个图像去目录和数据库...任何帮助,请?

这是我的表格

<form action="test.php" method="post" enctype="multipart/form-data">
Keywords: <input style="margin-left:35px;" type="text" name="keyword" /><br />
Name: <input style="margin-left:60px;" type="text" name="name" /><br />
Name2: <input style="margin-left:60px;" type="text" name="name2" /><br />
Categorie: <input style="margin-left:40px;" type="text" name="categorie" /><br />
Lieu: <input style="margin-left:70px;" type="text" name="lieu" /><br /><br />


<input type="file" name="file[]" multiple="multiple" /><input type="file"     name="file_2[]" multiple="multiple"/><input type="submit" name="submit" value="Upload" />




</form>

<?php



$connect = mysql_connect("localhost", "root", "");
$select_db = mysql_select_db("yakatrouver_test", $connect);

if(@$_POST['submit']){

$keywords = $_POST['keyword'];
$name = $_POST['name'];
$name2 = $_POST['name2'];
$categorie = $_POST['categorie'];
$lieu = $_POST['lieu'];





$file = $_FILES['file'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_size = $file['size'];
$file_path = $file['tmp_name'];

$file_2 = $FILES['file_2'];
$file_name_2 = $file['name'];
$file_type_2 = $file['type'];
$file_size_2 = $file['size'];
$file_path_2 = $file['tmp_name'];



if($file_name!="" &&     ($file_type="image/jpeg"||$file_type="image/png"||$file_type="image/gif") && $file_size<=2000000)
if(move_uploaded_file ($file_path, 'pictures_uploaded/' .$file_name))

$query = mysql_query("INSERT INTO `user_input`(keyword, name,  categorie, lieu) VALUES ('$keywords', '$name', '$categorie', ' $lieu')");    

$query = mysql_query("UPDATE `user_input` set image='pictures_uploaded/$file_name' WHERE `name`='$name'");
    if($query  == true)
    {
        echo "file Uploaded";

    }



}


$result = mysql_query("SELECT * FROM `user_input` WHERE `image`=''") or     die(mysql_error());

while($row = mysql_fetch_array($result))



?>

1 个答案:

答案 0 :(得分:0)

尝试: -

HTML:

<input type="file" name="file[]"/>
<input type="file" name="file[]"/>
<input type="file" name="file[]"/>

PHP:

$file_count = count($_FILES['file']['name']);
for($i=0;$i<$file_count;$i++) {
   $file_name = $_FILES['file']['name'][$i];
   $file_type = $_FILES['file']['type'][$i];
   $file_size = $_FILES['file']['size'][$i];
   $file_path = $_FILES['file']['path'][$i];

if($file_name!="" &&     ($file_type="image/jpeg"||$file_type="image/png"||$file_type="image/gif") && $file_size<=2000000)
if(move_uploaded_file ($file_path, 'pictures_uploaded/' .$file_name))

$query = mysql_query("INSERT INTO `user_input`(keyword, name,  categorie, lieu) VALUES ('$keywords', '$name', '$categorie', ' $lieu')");    

$query = mysql_query("UPDATE `user_input` set image='pictures_uploaded/$file_name' WHERE `name`='$name'");
   if($query  == true)
   {
    echo "file Uploaded";

   }
  }
} //close the braces accordingly, some seem to be missing in your code

我认为这是不言自明的。它只是循环遍历您的文件输入数组,获取每个文件信息并在一次处理一个。