在php中将一个表单中的图像数量插入数据库

时间:2014-05-14 21:06:14

标签: php mysql

我有大量的图片要上传到我的数据库,我以一种形式提交它的价值。 我想把这个函数调用到我的所有图像上传并在同一时间将它们插入我的数据库中...请我需要帮助

<form method="post" action="insert.php" enctype="multipart/form-data">
<tr><td>pic</td>
<td><input type="file" name="file" /></td></tr>
<tr><th>card </th>
<td><input type="file" name="cc" /></td></tr>
<input type="submit" name="submit" value="submit"  />
 </form>
    <?php
        include("connect.php");
        if(isset($_POST['submit']))
        {    
        function imgs(){
            var_dump($_FILES);
            $allowedExts = array("gif", "jpeg", "jpg", "png");
            $temp = explode(".", $_FILES["file"]["name"]);
            $extension = end($temp);
            if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/jpg")
            || ($_FILES["file"]["type"] == "image/pjpeg")
            || ($_FILES["file"]["type"] == "image/x-png")
            || ($_FILES["file"]["type"] == "image/png"))
            && ($_FILES["file"]["size"] < 2000000)
            && in_array($extension, $allowedExts)) {
              if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
              } else {
                echo "Upload: " . $_FILES["file"]["name"] . "<br>";
                echo "Type: " . $_FILES["file"]["type"] . "<br>";
                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
                if (file_exists("img/" . $_FILES["file"]["name"])) {
                  echo $_FILES["file"]["name"] . " already exists. ";
                } else {
                  move_uploaded_file($_FILES["file"]["tmp_name"],
                  "img/" . $_FILES["file"]["name"]);
                  echo "Stored in: " . "img/" . $_FILES["file"]["name"];
                }
              }
            } else {
              echo "Invalid file";
            }
            return $i="Stored in: " . "img/" . $_FILES["file"]["name"];}


            $pic=$_FILES['file']['name'];   
                $pic=imgs($_FILES['file']['name']);
            $sql1="insert into emp values ('', 'img\/".$pic."')";
             $r1=mysql_query($sql1,$con);
            if($r1){ echo "good";} else {echo "fail";}
                $cc=imgs($_FILES['cc']['name']);
                $sql2="insert into doc values ('','img\/".$cc."')";
            $r2=mysql_query($sql2,$con);
        ?>

        ?>

1 个答案:

答案 0 :(得分:0)

标题

html和php in data base

<html>
<body>
<form action="insert.php" method="post">
File: <input type="text" name="file">
Filename: <input type="text" name="filename">
<input type="submit">
</form>
</body>
</html>

<?php
   $con=mysqli_connect("example.com","acount","password","img");
     // Check connection
  if (mysqli_connect_errno()) 
  {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
    //tip  insert $_POST  to recive $_GET
$File = mysqli_real_escape_string($con, $_POST['file']);
$FileName = mysqli_real_escape_string($con, $_POST['filename']);
//insertin in to data base
$sql="INSERT INTO Persons (file, filename)
VALUES ('$File', '$FileName')";
//chequing for erros on database
   if (!mysqli_query($con,$sql)) 
   {
     die('Error: ' . mysqli_error($con));
   }
   echo "1 record added";

    //close database
  mysqli_close($con);
?>